MySQL - how to return a list of iD number from another table excluding a specific ID number

时间:2015-06-15 15:10:38

标签: mysql sql

I'm making a performance evaluation system. This is my example database:

Table: Person
ID     Name
--     ---
1      James
2      John
3      Jake

Table: Evaluation
-------  --  ------------
Eval_ID  ID  Evaluator_ID
0        1       2

I need a query where I can find a list of ID number from person that has not been evaluated by the evaluator_ID. So for example the ID number 1 would not be displayed from the result since it has been already evaluated by 2. So only the ID number 3 will be displayed because you cannot evaluate yourself.

2 个答案:

答案 0 :(得分:1)

Use NOT EXISTS

SELECT p.ID, p.Person
FROM Person p
WHERE p.ID NOT EXISTS (SELECT e.ID FROM Evaluation e WHERE p.ID = e.ID OR p.ID = e.Evaluator_ID)

答案 1 :(得分:0)

SELECT p.ID, p.Name
FROM Person p
WHERE p.ID NOT in (SELECT e.ID FROM Evaluation e group by e.id )
 and   p.ID NOT in (SELECT f.Evaluator_ID FROM Evaluation f group by f.Evaluator_ID )