Here are my two tables:
[items]
- id - model - location_id -
1 mA 23
2 mA 23
3 mA 23
4 mB 24
5 mB 24
6 mC 25
7 mC 26
[locations]
- id - name -
23 aisle-3
24 aisle-4
25 aisle-5
26 aisle-6
I am trying to query the locations table for the location names and also bring back a count of the items at that location. Here is something I tried to no avail:
SELECT name, COUNT(item.id)
FROM locations
INNER JOIN items AS item ON (item.location_id = locations.id)
Can anyone help me with this?
You forgot to GROUP BY:
SELECT l.*, COUNT(item.id)
FROM locations l
INNER JOIN items AS i
ON i.location_id = l.id
GROUP BY l.id
And if you want to get COUNT() even when there is no item assigned to that location you should LEFT JOIN instead of INNER JOIN.
Related
I am using Php with join, I have two tables ("services" and "service_detail"), I want to get all services
but want to know which service selected or notselected by vendor
Here is my services table strcture
id service_name image
1 Abc abc.jpg
2 xyz xyz.jpg
3 OPS ops.jpg
4 tys tys.jpg
5 byp byp.jpg
Here is my services_detail table strcutre
id vendor_id service_id price
1 101 1 50
2 101 2 70
3 101 3 80
4 101 4 30
5 102 1 70
6 102 2 40
...
I tried with following query but showing only selected services, but i want to get all services with parameter ( selected or notselected)
Where i am wrong ? Here is my query
SELECT sd.vendor_id, sd.service_id, sd.price, s.service_name, s.image
FROM services_detail sd
LEFT JOIN services s
ON sd.service_id = s.id
WHERE sd.vendor_id = '101'
Move your where clause to be AND in ON clause:
AND sd.vendor_id = '101'
And interchange tables in join to get all servcies
SELECT sd.vendor_id, sd.service_id, sd.price, s.service_name, s.image,
IF (sd.vendor_id is not null, 'Opted', 'Not opted') as status
FROM services s
LEFT JOIN services_detail sd
ON sd.service_id = s.id AND sd.vendor_id = '101';
In simple words, when there is a where clause including filters on table of Left Join then it will act like INNER JOIN not LEFT JOIN.
I need data from multiple tables using joins. Below are the tables.
table 1 : list_vehicles
pk_vehicle_id vehicle_reg_no vehicle_type
1 REG1 Bus
2 1 Bus
7 1 Bus
table 2 : list_vehicles
pk_route_id route_code route_name route_description
26 CODE1 Route1 First Route
27 CODE2 Route2 Second Route
28 CODE3 Route3 Third Route
table 3 : tbl_route_vehicle_mgmt
pk_route_veh_id fk_route_id fk_vehicle_id
4 22 2
5 23 1
6 27 1
table 4: tbl_staff_allocation
pk_id fk_route_id fk_staff_id staff_type
1 27 13 Attendant
2 27 14 Driver
3 27 15 Conductor
I need the following data from the above tables, say for pk_route_id =27
Route_Name Vehicle_Number Vehicle_Type Driver_Id Attendant_Id Conductor
Route 2 REG1 Bus 13 14 15
I tried to write a part of the SQL as follows. I am stuck and not sure how to enhance it to get the required results.
SELECT a.route_code,a.route_name,a.route_description, tbl_b.fk_vehicle_id,tbl_c.fk_staff_id,tbl_c.staff_type, tbl_c.fk_route_id
FROM `list_routes` AS a
INNER JOIN tbl_route_vehicle_mgmt AS tbl_b
ON a.pk_route_id = tbl_b.fk_route_id
INNER JOIN tbl_staff_allocation AS tbl_c
ON a.pk_route_id = tbl_c.fk_route_id
where a.pk_route_id =27 AND (tbl_c.staff_type ="Driver" OR tbl_c.staff_type ="Conductot" OR tbl_c.staff_type ="Attendant" )
Can anyone please help me with the SQL to get the required data.
You should use several self join on tbl_staff_allocation
SELECT
a.route_code
,a.route_name
,a.route_description
,tbl_b.fk_vehicle_id
,tbl_c1.fk_route_id
,tbl_c1.fk_staff_id as Attendant_id
,tbl_c2.fk_staff_id as Driver_id
,tbl_c3.fk_staff_id as Conductor_id
FROM `list_routes` AS a
INNER JOIN tbl_route_vehicle_mgmt AS tbl_b ON a.pk_route_id = tbl_b.fk_route_id
INNER JOIN tbl_staff_allocation AS tbl_c1 ON a.pk_route_id = tbl_c1.fk_route_id and tbl_c1.staff_type ='Attendant'
INNER JOIN tbl_staff_allocation AS tbl_c2 ON a.pk_route_id = tbl_c2.fk_route_id and tbl_c.staff_type ='Driver'
INNER JOIN tbl_staff_allocation AS tbl_c3 ON a.pk_route_id = tbl_c3.fk_route_id and tbl_c.staff_type ='Conductor'
INNER JOIN list_vehicles AS d on d.pk_vehicle_id = tbl_b.fk_vehicle_id
Let's assume we have 2 tables:
First table: "members"
id name
===========
10 Rooney
20 George
30 Hoytt
40 Percy
Second table: "interactions"
id iType mem1 mem2
===========================
5501 PRIVMSG 10 30
5502 NOTICE 20 40
And the result should be:
id iType mem1 mem2 mem1name mem2name
==========================================
5501 PRIVMSG 10 30 Rooney Hoyyt
5502 NOTICE 20 40 George Percy
How can we achive this output table using a single MySQL query?
Thanks by now.
Just use two joins and give them differen aliasses like this:
SELECT interactions.*,m1.name as 'mem1name',m2.name as 'mem2name'
FROM
interactions
LEFT JOIN members m1 ON (interactions.mem1 = m1.id)
LEFT JOIN members m2 ON (interactions.mem2 = m2.id)
I'm having a lot of trouble figuring out how to write this query. Here's an exmaple of the data set and what I need to query:
**System Table**
SystemID Active
1 T
2 T
3 T
4 F
5 F
6 F
**BlogPost Table**
BlogPostID SystemID Create_Month
100 2 Jan
101 2 Jan
102 2 Feb
103 3 Feb
104 3 Mar
105 6 Mar
106 6 Mar
**Comment Table**
Comment ID BlogPostID Liked
201 100 T
202 100 T
203 100 T
204 102 T
205 102 T
206 102 T
207 103 F
So, In words, I'm trying to get: By month, show me all the active systems who created a post during that month, the number of posts they made in aggregate, and the count of the subset of those posts who had a comment that was like.
The end result would be like:
Column 1 - Month
Column 2 - Count of Active Systems where a Post Created in Month
Column 3 - Count of Posts Applicable to those systems
Column 4 - Count of Applicable Posts that had comments that were liked
I don't even know where to start really. My terrible "this is obviously wrong" attempt is below. Any help is much appreciated, thanks!
SELECT
Month,
COUNT(DISTINCT system.systemid),
COUNT(blogpost.BlogPostID)
COUNT(comments.commentiD)
FROM
system INNER JOIN
blogpost ON system.systemid = blogpost.systemid INNER JOIN
comments ON blogpost.BlogPostID = comment.BlogPostID
WHERE
system.active = T
AND comments.like = T
GROUP BY month
A complicated one !
SELECT
b.Create_Month,
COUNT(DISTINCT s.SystemID) as SystemCount,
COUNT(DISTINCT b.BlogPostID) as PostsCount,
COUNT(DISTINCT t.BlogPostID) as PostsWithLike
FROM System s
JOIN BlogPost b
ON s.systemID = b.systemID
AND s.Active = 'T'
LEFT JOIN Comment c
ON b.BlogPostID = c.BlogPostID
LEFT JOIN
(
SELECT DISTINCT c.BlogPostID as BlogPostID
FROM
Comment c
GROUP BY c.BlogPostID
HAVING SUM(if(c.Liked='T',1,0))>0
) as t
ON b.BlogPostID = t.BlogPostID
GROUP BY b.Create_Month
This is probably what you want :
SELECT s.systemid, active, bp.create_month, bp.systemid, COUNT(bp.blogpostid), COUNT(c.liked)
FROM system AS s
LEFT OUTER JOIN Blogpost AS bp ON s.systemid = bp.systemid
LEFT OUTER JOIN Comment AS c ON bp.blogpostid = c.blogpostid
WHERE active = 'T' AND c.Liked = 'T' GROUP BY s.systemid,bp.create_month
I need to perform a COUNT on a quite a big query, where one of the joined tables has a one-to-many relationship. This is throwing off my result as all data is being multiplied by the number of times an item is repeated in the 'many' side of the one-to-many table.
This is a shortened version of the query showing only the relevant portion to highlight the issue:
SELECT COUNT(trimtype) FROM versiontrim
INNER JOIN trims USING (trim_id)
INNER JOIN prices USING(version_id)
INNER JOIN m_versions USING(version_id)
WHERE trimtype IN('sec', 'help') AND price BETWEEN 200001 AND 210000
GROUP BY version_id
All tables are quite straighforward except m_versions that has the one-to-many relationship and looks like this:
version_id serv_id
1 1
1 2
1 3
1 4
1 5
.... and so on
The expected result of the query is :
version_id COUNT(trimtype)
44 9
54 7
69 9
214 10
216 6
282 1
290 10
Instead I am getting this,ie, all counts multiplied by 5 which is the number of times version_id is repeated in the m_versions table:
version_id COUNT(trimtype)
44 45
54 35
69 45
214 50
216 30
282 5
290 50
How to avoid this behavior?
Thanks
It matches to multiple records on table m_version that is why you are getting invalid result. Try wrapping it a subquery,
INNER JOIN (SELECT DISTINCT version_id FROM m_versions) m USING(version_id)
UPDATE
So the full query will look like this,
SELECT version_id, COUNT(trimtype)
FROM versiontrim
INNER JOIN trims USING (trim_id)
INNER JOIN prices USING(version_id)
INNER JOIN (SELECT DISTINCT version_id FROM m_versions) m USING(version_id)
WHERE trimtype IN('sec', 'help') AND price BETWEEN 200001 AND 210000
GROUP BY version_id