开发者

MySQL query - Getting people out that arent in another table?

i hope i explain this correctly.

What i have is this query..

SELECT *  FROM classesbooked 
JOIN name ON NameNo = cla开发者_如何学JAVAssesbooked_nameno
Group By classesbooked_nameno

This gets me out all the names of people who have booked a class. What i want is the opposite. All the people who are in table 'name' but not in table 'classesbooked'

?


Use a left join and select records where the second table's join column is null.

select *
from name n
    left join classesbooked c on n.NameNo = c.classesbooked_nameno
where c.classesbooked_nameno is null


One of the ways you can accomplish this is with a sub query:

SELECT *
FROM name
WHERE NameNo NOT IN (
                       SELECT 
                          classesbooked_nameno
                       FROM classesbooked
                    )

Essentially this says to return everything in table name that does not have an associated Id in classesbooked


Try this:

   Select * from name tbl
        where tbl.NameNo 
        not in (select t.classesbooked_nameno 
                 from classesbooked_nameno t)


SELECT * 
FROM name 
  LEFT JOIN classesbooked 
    ON NameNo = classessbooked_nameno 
WHERE classesbooked_nameno IS NULL
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜