简体   繁体   中英

LEFT JOIN doesn't work with count

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;

Try this

This query will fetch common names from table 1 and table 2. It will also fetch names from table 2 which are not there in table 1.

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

Let me if this works.

I would do a simple in check then

SELECT table1.Name,
FROM table1
WHERE table1.Name In (SELECT table2.Name_all FROM table2)
GROUP BY table1.Name

There are other ways, but without more information this is a simple way t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM