有两个表 问题一:表A里面有 有一个字段是item_id 包含一些项
表B里面也 有一个字段是item_id 包含一些项我想找到所有的item_id的值, 这些值存在于表A里面, 但是不存在于表B中
请问怎么写?问题二:
表A里面有 有一个字段是item_id 包含一些项
表B里面也 有一个字段是item_id,还有一个字段是item_value 包含一些项我想找到所有的item_id的值, 这些值存在于表A里面, 但是不存在于表B中, 或者即使存在于表B中,其对应的item_value 均 <3注意 表B的item_id不是唯一的,即可能存在这种情况item_id     item_value
10020          1
10020          3
10020          2
10021          3
10021          0所以找到的item_id的值,只要有一项对应的item_value >=3 就不复合条件了请问这个sql语句怎么写,谢谢!

解决方案 »

  1.   

    问题一。  select item_id from A where item_id not in (selet item_id from B)
    问题二。 select item_id from A where item_id not in (select item_id from b)  union select item_id 
    from a ,b where a.item_id =b.item and a.item_value <3
      

  2.   

    问题2: try
    select item_id from a where item_id not in (select item_id from B)
    union
    select item_id from a where item_id in(
    select bb.item_id from(
    select max(item_value) aa, item_id from B group by item_id having aa<3)bb);
      

  3.   

    1、select A.item_id  from A left join B using(item_id) where B.item_id is null;
      

  4.   

    2、
    select A.item_id  from A left join B using(item_id) where B.item_id is null or B.item_value < 3;