Select * From employee Left Join department On employee.dno=department.dno Where department.dno Is Null
在MySQL4.1可能会支持, 大约今年就可以出来了吧
但如果情况如下: 两个表,一个是零件表part,pid是其中的主键,表示零件号 另外一个表是订单表order,有三个字段,一个是主键oid(订单号), 一个是零件号(pid),还有一个是厂商号(fid)那么如果我要做如下查询: 找出厂商号为2的厂商所不生产的所有零件的价格(price)那么如果可以使用not in形式的语句为: select price from part where pid not in (select pid from order where fid=2)但是如果用join怎么办呢?
select part.pid,part.price from part LEFT JOIN order ON part.pid=order.pid where (order.fid = 2) and (order.pid is Null);
分开两条语句执行吧,先用 select pid from order where fid=2 得到$in_str 再执行 select price from part where pid not in ($in_str)
我一开始的时候就是使用binscut的那种方法 这当然是可行的 但是难道没有替代方法吗?
select p.pid,p.price from part as p left join order as o on p.pid=o.pid and o.fid=2 where o.fid is null;你把表的关系掉过来想,让PART JOIN ORDER ,就解决了 注意join-condition中的o.fid和where 中的o.fid是不一样的, 前者是join 条件,后者是结果条件
大约今年就可以出来了吧
两个表,一个是零件表part,pid是其中的主键,表示零件号
另外一个表是订单表order,有三个字段,一个是主键oid(订单号),
一个是零件号(pid),还有一个是厂商号(fid)那么如果我要做如下查询:
找出厂商号为2的厂商所不生产的所有零件的价格(price)那么如果可以使用not in形式的语句为:
select price from part where pid not in (select pid from order
where fid=2)但是如果用join怎么办呢?
LEFT JOIN order ON part.pid=order.pid
where (order.fid = 2) and (order.pid is Null);
select pid from order where fid=2
得到$in_str
再执行
select price from part where pid not in ($in_str)
这当然是可行的
但是难道没有替代方法吗?
注意join-condition中的o.fid和where 中的o.fid是不一样的,
前者是join 条件,后者是结果条件
这个不好
因为这个问题很值得讨论,大家还可以讨论嘛
我建议是否可以放到精华区或者faq呢?
欢迎大家来讨论
明天结贴吧
到时楼上各位均有分
在此谢谢各位了
请给出SQL,多谢