SQL-列出没有贡献的人

时间:2013-10-15 02:20:04

标签: mysql sql sql-server sql-server-2008 tsql

我有一个SQL任务,它要求“显示名字和姓氏的人员列表,他们没有贡献任何作品。”

这是我尝试过的,但无济于事。

select * from (select count(a.person_id)num,a.first_name ||' '|| a.last_name name
  from LABPRJ_PERSON a, LABPRJ_COMPOSITION_DETAIL w
  where a.person_id=w.person_id group by last_name, a.first_name
  union
  select 0,a.first_name ||' '|| a.last_name name from LABPRJ_PERSON a where person_id not in (select person_id
  from LABPRJ_COMPOSITION_DETAIL)) where num = 0; 

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:0)

据我所知,你应该只需要这个:

SELECT * FROM LABPRJ_PERSON WHERE person_id NOT IN (SELECT person_id FROM LABPRJ_COMPOSITION_DETAIL);

答案 1 :(得分:0)

您还可以使用NOT EXISTS,其效果优于NOT IN

SELECT last_name, first_name FROM LABPRJ_PERSON p
WHERE NOT EXISTS
    (SELECT * FROM LABPRJ_COMPOSITION_DETAIL c ON c.person_id = p.person_id)

答案 2 :(得分:0)

如果你使用的是SQL SERVER,请尝试一下。

SELECT
  CAST(A.FIRST_NAME AS  AS VARCHAR(MAX))  + ' '+  CAST(A.LAST_NAME AS VARCHAR(MAX))
  FROM 
      LABPRJ_PERSON A
  WHERE NOT EXISTS 
  (
    SELECT 
        B.PERSON_ID 
    FROM
        LABPRJ_COMPOSITION_DETAIL B WHERE A.PERSON_ID = B.PERSON_ID
  ) 

答案 3 :(得分:0)

SELECT last_name, first_name 
FROM LABPRJ_PERSON AS T1
RIGHT OUTER JOIN
LABPRJ_COMPOSITION_DETAIL AS T2
ON T1.person_id=T2.person_id
WHERE T2.person_id IS NULL;