Tuesday 4 September 2018

Mysql LEFT JOIN does not work with account

I have a problem with my query:

SELECT table1.Name,
       COUNT(*)
FROM table1
LEFT JOIN table2 ON table2.Name_all = table1.Name
GROUP BY table1.Name

It shows and counts a names from table1. I want to join all names from table2 which do not exist in table1.
Maybe someone knows what I am doing wrong?
Thanks in advance

From your description you seem to mean this, which is a list of name_all that does not match table1 name.
SELECT table2.Name_all
FROM table2
LEFT JOIN table1 ON table2.Name_all = table1.Name
WHERE table1.Name Is Null

If you need a count as well, you can say:
SELECT table2.Name_all, Count(table2.Name_all) AS CountOf
FROM table2
LEFT JOIN table1 ON table2.Name_all= table1.Name
WHERE table1.Name Is Null
GROUP BY table2.Name_all;

0 comments:

Post a Comment