日期:2014-05-19  浏览次数:20388 次

一条查询语句,帮忙看看
表1:
truck_code               chg_code       amount
7022                         拖车费   670
7022                         提箱费   75
6892                         工资   50
6892                         佣金   30
6892                         港建费   30
6892                         油费   100

表2:
truck_code chg_code
6892                   过路费
6892                   修理费
7022                   油费
7022                   吊箱费
6892                   工资
7022                   佣金

显示如果如下:
7022                         拖车费   670
7022                         提箱费   75
6892                         港建费   30
6892                         油费   100

也就是说,只要不在表2中的都显示出来;条件是根据truck_code和chg_code;
请帮忙下,谢谢!~~

------解决方案--------------------

select * from A
where not exists(select 1 from B where truck_code=A.truck_code )
------解决方案--------------------
select * from 表1 a where NOT EXISTS(select * from 表2 b where a.truck_code=b.truck_code and a.chg_code = b.chg_code)
------解决方案--------------------
select a.*
from A left join b on a.truck_code = b.truck_code
where b.truck_code is null
------解决方案--------------------
select * from table1 ajoin (select truck_code, chg_code from table1 where not exists select * from table2)b on a.truck_code=b.truck_code and a.chg_code=b.chg_code