Im trying to join 3 tables together using left join.
My problem is that I dont get the right join for my query.
The number of return rows is different.
The count is not correct.
If I run this I get correct join and more than 1 row.
SELECT classes.name, trainers.trainer_name trainerName
FROM gym_classes classes
left join gym_tainer_rel trainers
on
classes.teacher = trainers.id
But If I try this query:
SELECT classes.name, trainers.trainer_name trainerName, count(usersInClass.user_name) as sum
FROM gym_classes classes
left join gym_tainer_rel trainers
on
classes.teacher = trainers.id
left join class_user_rel usersInClass
on
usersInClass.class_id = classes.id
I get only 1 row back and the count is not correct.
Related
Can someone help me to understand those results ? (For me all 3 should return 6455).
(Using RDS mysql-8.0.13)
SELECT COUNT(p.product_id) FROM product p LEFT JOIN product_attributes pa ON p.pdt_id = pa.pdt_id WHERE pa.code = 'season';
Results : 6332
SELECT COUNT(*) FROM product p;
Results : 6455
SELECT COUNT(p.product_id) FROM product p LEFT JOIN product_attributes pa ON p.pdt_id = pa.pdt_id AND pa.code = 'season';
Results : 6455
Your first join uses the WHERE clause, this mean sit selected all the rows, including those with a null join and then filters out those WHERE the pa.code = season, i.e. the null joins.
The last one joins on both, but because it is a left join you still get the full table of results, and nothing is filtered because you remove the WHERE clause. If you were to use an INNER JOIN in the last query you should get the same result (6332).
This link might be useful What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
I need to get data from 3 tables. However, I can only get correct results as long as there are 2 tables. As soon as I join the 3rd table, I get null results. The query that works is:
select a.first_name,a.last_name,a.email, a.birthdate,b.mobile_phone
from REGISTRATION a
left join TRIPS b on a.trip_id=b.trip_id
where a.trip_id=9 and registration_status='Active'
However, as soon as I use following query to get data from 3rd table, I get null results:
select a.first_name,a.last_name,a.email, a.birthdate,b.mobile_phone,d.country_name
from REGISTRATION a
left join TRIPS b
on a.trip_id=b.trip_id and registration_status='Active'
left join DESTINATION_COUNTRY d
on b.destination_country_id=d.destination_country_id
where a.trip_id=9
Please advice me what I am doing wrong.
Thanks a lot.
you should remove this from join "and registration_status='Active'" and move it to where clause :
select a.first_name,a.last_name,a.email, a.birthdate,b.mobile_phone,d.country_name
from REGISTRATION a
left join TRIPS b
on a.trip_id=b.trip_id
left join DESTINATION_COUNTRY d
on b.destination_country_id=d.destination_country_id
where a.trip_id=9 and registration_status='Active'
SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, invoices.customer_id, purchaseorders.vendor_id FROM items
INNER JOIN (products, invoices, purchaseorders)
ON (items.product_id=products.product_id AND items.invoice_id=invoices.id
AND items.po_id=purchaseorders.id)
This returns nothing... however..
SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, purchaseorders.vendor_id FROM items
INNER JOIN (products, purchaseorders)
ON (products.product_id=items.product_id AND purchaseorders.id=items.po_id)
Works...
SELECT products.acctnum,products.subacctnum,NOW(),
items.amount,items.id,items.invoice_id,items.product_id,
items.po_id, invoices.customer_id FROM items
INNER JOIN (products, invoices)
ON (products.product_id=items.product_id AND invoices.id=items.invoice_id)
Works...
Works for the rows I need in the result but when I join the 3rd table it doesn't work. LEFT JOIN displayed all the columns I needed but some rows were NULL.
I imagine the join clause you want looks more like this:
FROM items LEFT JOIN
invoices
ON invoices.id = items.invoice_id LEFT JOIN
purchaseorders
ON purchaseorders.id = items.po_id LEFT JOIN
products
ON products.product_id = items.product_id
I'm not sure which fields are not valid when you select them, but you can probably fix such issues by using coalesce() with appropriate fields from invoices and purchaseorders.
I have the following database example:
The example is pretty much self-explanatory: There are lessons held by teachers at defined time periods (time_start, time_end) each time period -> lesson connection has its own max_students number.
I know want to list all lessons with all information of the 3 tables (and the max_students). I would do it like that (I heard, that joining table like that is the fastest way):
SELECT * FROM lesson, teacher, time, teacher_has_lesson, time_has_lesson
WHERE lesson.lesson_id = teacher_has_lesson.lesson_lesson_id
AND teacher.teacher_id = teacher_has_lesson.teacher_teacher_id
AND lesson.lesson_id = time_has_lesson.lesson_lesson_id
AND time.time_id = time_has_lesson.time_time_id
1.) Is this a good solution if you just want to join 3 tables or are there better ones?
2.) This SQL call will get me only lessons, that have a teacher and a time. I also want to display lessons, that are in the database, but dont have a teacher or time yet. How can I do that?
There's an alternative way of writing this using join syntax. What you have is equivalent to an inner join, where you only see rows where there are matches:
select
*
from
lesson l
inner join
teacher_has_lesson tl
on l.lession_id = tl.lesson_lesson_id
inner join
teacher t
on tl.teacher_teacher_id = t.teacher_d
inner join
time_has_lesson tml
on l.lesson_id = tml.lesson_lesson_id
inner join
time tm
on tml.time_time_id = tm.time_ud
There's another type of join called outer join, where all the rows from one table are shown, and null values supplied if there are no matching values in the other table. It comes in two or three variants. left outer join shows all rows from the first table. right outer join shows all rows from the second table. full outer join shows all rows from both tables. So, for your second query you could use:
select
*
from
lesson l
left outer join
teacher_has_lesson tl
on l.lession_id = tl.lesson_lesson_id
left outer join
teacher t
on tl.teacher_teacher_id = t.teacher_d
left outer join
time_has_lesson tml
on l.lesson_id = tml.lesson_lesson_id
left outer join
time tm
on tml.time_time_id = tm.time_ud
I am trying to select data from 3 separate tables, where 2 of those contains multiple rows of data. The sql query i am using is this, but i am experiencing that when i run it, if either taskdependees or tasksdependencies have zero result, the whole task is not showing.
SELECT t.*, GROUP_CONCAT(a.DependenciesId) as DiesId, GROUP_CONCAT(b.DependeesId) as DeesId FROM tasks t JOIN tasksdependencies a ON a.TasksId=t.TasksId JOIN taskdependees b ON b.TasksId=t.TasksId GROUP BY t.TasksId
What am I doing wrong in this query?
Use LEFT JOIN , inner join will give row if there is association is present in both tables while left will return the rows from left table even if they are not associated
SELECT t.*, GROUP_CONCAT(a.DependenciesId) as DiesId,
GROUP_CONCAT(b.DependeesId) as DeesId
FROM tasks t
LEFT JOIN tasksdependencies a ON a.TasksId=t.TasksId
LEFT JOIN taskdependees b ON b.TasksId=t.TasksId
GROUP BY t.TasksId