日期:2014-05-18  浏览次数:20531 次

SQL 1999 标准中“打印”语句的写法
请诸位高手指教这道题的做法:
给出一个SQL:1999查询语句,打印既不是职员(employee)又不是客户(customer)的人员(person)的姓名(name)。

  1. Table "person":
  field: person_id (key), name, street, city
  2. Table "employee":
  field: salary
  3. Table "customer"
  field: credit_rating
  "employee"和"customer"都是"person"的字表。


------解决方案--------------------
SQL code

select a.name
from perso a
left join employee b on a.person_id=b.person_id
left join customer c on a.person_id=c.person_id
where b.person_id is null and c.person_id is null

------解决方案--------------------
select * from person where employee <> ... and customer <> ....
------解决方案--------------------
探讨
select * from person where employee <> ... and customer <> ....

------解决方案--------------------
楼主说的"打印"是指前端程序的报表呈现吧?

把查询的结果集返回给前端程序,然后用报表工具显示出来即可.

------解决方案--------------------
此写法逻辑上等价于where xx not in(select ...)
但执行效率高于后者.