1

我有一个 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

4 回答 4

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;
于 2013-10-15T05:35:40.687 回答
0

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

SELECT * FROM LABPRJ_PERSON WHERE person_id NOT IN (SELECT person_id FROM LABPRJ_COMPOSITION_DETAIL);
于 2013-10-15T02:21:57.543 回答
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
  ) 
于 2013-10-15T05:23:52.610 回答
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)
于 2013-10-15T03:18:43.220 回答