MySql Count with Join - mysql

I have two tables -
Users_tag (userID,tagID)
tagId - userId
1 1
2 1
3 2
4 2
5 4
Tags (id,tagText)
id tagText
1 dog
2 cat
3 lion
4 tiger
5 chicken
I want my query to return tagId,TagText and count of each row based on userId.
Any help please...

SELECT a.id, a.tagText, COUNT(b.tagID) totalCount
FROM Tags a
LEFT JOIN users_tag b
on a.ID = b.tagID
GROUP BY a.id, a.tagText
SQLFiddle Demo

Related

How to Combine GROUP CONCAT with COUNT in MySQL?

I am trying to write a query that can get result from multiple tables:
Item Category
ic_id
ic_name
1
PC
2
Laptop
3
Printer
4
Scanner
Items
i_id
i_category
i_name
1
1
Dell Optiplex
2
2
HP Probook 450
3
2
HP Probook650
4
3
HP Laserjet 402dn
5
1
Dell MT3030
Item Sale
is_id
is_date
is_customer
1
15-03-2021
John
2
16-03-2022
Jimmy
3
18-03-2023
Mark
Item Sale Detail
isd_id
isd_sale_id
isd_item
1
1
2
2
1
3
3
2
4
4
3
1
5
3
5
6
3
4
Is it possible to get combined result of GROUP CONCAT with COUNT in 1 query? Please guide me to write the query to get the desired result, I want the query result as shown below, Thanks:
Desired Result
is_id
is_date
is_customer
items
1
15-03-2021
John
Laptop: 3
2
16-03-2022
Jimmy
Printer: 1
3
18-03-2023
Mark
PC:2 , Printer: 1
If I understand correctly, this is just join but with two levels of aggregation:
select its.*, ic.categories
from item_sale its join
(select isd.isd_sale_id,
group_concat(ic_name, ':', cnt order by cnt desc) as categories
from (select isd.isd_sale_id, ic.ic_name, count(*) as cnt
from item_sale_detail isd join
items i
on isd.isd_item = i.i_id join
item_category ic
on i.i_category = ic.ic_id
group by isd.isd_sale_id, ic.ic_name
) ic
group by isd.isd_sale_id
) ic
on i.is_id = ic.isd_sale_id;

Finding all or none

I have four tables detailing an amusement park and its guests' ride history.
Categories
c_id name
1 Thrill
2 Leisure
3 Kiddie
Rides
r_id c_id
1 1
2 1
3 2
4 2
5 3
6 3
guest_history
h_id g_id
1 1
2 1
3 2
4 3
history_items
h_id r_id
1 5
2 6
3 1
3 2
4 5
How would I get all of the guests (g_id's) that have either rode all of the kiddie rides or none of the kiddie rides?
Expected Output would be:
g_id
1
2
I can't seem to figure out what the easiest way to go about it would be. I can only seem to conjure up a table that contains all of the cases that a guest has rode a kiddie ride. My attempt was a 4-way inner join of the tables and to filter out on the c_id = "Kiddie". Any help would be appreciated.
You can try this below logic-
3 is fixed in the query to get category "Kiddie"
DEMO HERE
SELECT A.g_id,COUNT(C.r_id)
FROM guest_history A
INNER JOIN history_items B ON A.h_id = B.h_id
INNER JOIN Rides C ON B.r_id = C.r_id AND C.c_id = 3
GROUP BY A.g_id
HAVING COUNT(DISTINCT B.r_id) = (SELECT COUNT(r_id) FROM Rides WHERE c_id = 3)
OR COUNT(DISTINCT B.r_id) = 0
SELECT g_id
FROM Categories
NATURAL JOIN Rides
NATURAL JOIN guest_history
NATURAL JOIN history_items
GROUP BY g_id
HAVING COUNT(DISTINCT r_id) IN (0,
(SELECT COUNT(DISTINCT r_id)
FROM Categories
NATURAL JOIN Rides
WHERE name = 'Kiddie')
)
fiddle

How Sequence Matters in Laravel query builder for mysql

The complied query has different sequence from the code.
The query works but I don't know why it works!
Here is the business:
I get the roomId by the $userId, and then I get all the playerId by the roomId
Steps:
in Table room_player_relations, I get a row where playerId = $userId.
in Table room_player_relations, I get all the rows where roomId = the roomId of the row found in point 1.
select playerId from the rows found in point 2.
Here is the code:
$players = DB::table('room_player_relations as t1')
->where('t1.playerId',$userId)
->join('room_player_relations as t2','t1.roomId','=','t2.roomId')
->select('t2.playerId')
->get();
Here is the query seen in the debugbar:
select `t2`.`playerId` from `room_player_relations` as `t1`
inner join `room_player_relations` as `t2`
on `t1`.`roomId` = `t2`.`roomId`
where `t1`.`playerId` = '1'
It looks stupid because it join 2 the same tables together, the where is put afterward. But it works. Please tell me why? Thanks!
I find out INNER JOIN-ing two table is not sticking two tables together, but matching all the possible ways of the matching key (roomIdin my example). I can actually see the result of INNER JOIN by the following query:
select * from `room_player_relations` as `t1`
inner join `room_player_relations` as `t2`
on `t1`.`roomId` = `t2`.`roomId`
playerId roomId playerId roomId
2 2 2 2
3 2 2 2
1 2 2 2
2 2 3 2
3 2 3 2
1 2 3 2
4 1 4 1
5 1 4 1
4 1 5 1
5 1 5 1
2 2 1 2
3 2 1 2
1 2 1 2

Get most sold products with a specified one

I have a table order_detail with this informations
id_order id_product
1 2
1 3
2 2
2 4
2 5
2 3
3 2
3 1
4 2
4 3
4 1
4 6
I would like to get most sold products(the first 5 elements) with the current product let's say product id 2
I tried this but it returns one wrong result
SELECT od2.product_id, count(od2.`product_id`) FROM `ps_order_detail` od1
LEFT JOIN ps_order_detail od2 ON od1.id_order = od2.id_order where
od2.product_id != od1.product_id AND od1.product_id=2
The result should be
product_id count(od2.`product_id`)
3 3
4 2
1 1
5 1
6 1
Your query is on the right track. Mostly, you are missing a group by:
select od.product_id, count(od2.id_order) as NumTimesWith2
from ps_order_detail od left join
ps_order_detail od2
on od.id_order = od2.id_order and
od2.product_id = 2
where od.product_id <> 2
group by od.product_id
order by count(od2.id_order) desc;
If you want only one such product, then add a limit 1 to the query.
Also, this assumes that products are not repeated inside orders. If they can be, you can quickly get a better count using count(distinct od.id_order)).

How to join two tables with two conditions?

I have two tables:
LLOAN
LOANID SOURCEID LOAN_COMPANY ETC
1 1 3
2 1 3
3 1 1
4 2 1
5 2 1
6 2 1
7 3 1
8 3 1
COMPANY
CompanyID CountryID CompanyIDLLAS
1 1 1
2 1 2
3 1 3
4 2 1
5 3 1
6 4 1
And I want to join them. The SourceID refers to the CountryID and the LOAN_COMPANY refers to the CompanyID. Only country '1' has multiple companies, all the others just have one.
How can I join these two tables correctly? I've tried many different things, of which this came the closest:
SELECT Count(c.CompanyID) FROM dbo.LLOAN As l
LEFT JOIN dbo.Company As c ON c.CountryID = l.SourceID AND c.CompanyID = l.LOAN_COMPANY
But it leaves many rows blank. What is the correct way to join two tables with two conditions?
Try below Query:
SELECT Count(c.CompanyID)
FROM dbo.LLOAN As LL
LEFT JOIN dbo.Company As C
ON (C.CountryID = LL.SourceID)
AND (C.CompanyID = LL.LOAN_COMPANY)
You can group the condition using paranthesis like this:
SELECT Count(c.CompanyID)
FROM dbo.LLOAN As l
LEFT JOIN dbo.Company As c ON (c.CountryID = l.SourceID) AND (c.CompanyID = l.LOAN_COMPANY)