Answer the question
In order to leave comments, you need to log in
SQl JOIN thinks weird?
Guys, hello.
I ran into a problem, JOIN strangely counts the number of records in tables.
Task: you need to count the number of records with the desired id in several tables.
I am building a query, it seems to count, but it is the output that does not form correctly.
SELECT t.id,t.master_theme, t.theme, COUNT(post.theme_id) as post_theme, COUNT(video.theme_id) as video_theme, COUNT(subscribe_theme.theme_id) as subscribe_theme
FROM `theme` as t
LEFT OUTER join post ON t.id = post.theme_id
LEFT OUTER join video ON t.id = video.theme_id
LEFT OUTER join subscribe_theme ON t.id = subscribe_theme.theme_id
WHERE t.id in (88,14,91,23,79,13,21,24,89,17,65,35,97,82,37,38,85,64,56,90,60,99,16,76)
GROUP BY t.id
Answer the question
In order to leave comments, you need to log in
JOIN - Cartesian multiplication.
One line from `theme`, two from `post s` to it, zero from `video` is ignored (LEFT), one from `subscribe`
Multiply, 1*2*1*1 = 2, which is what you get.
If there are two lines in `subscribe`, we get 1*2*1*2 = 4
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question