语句一:
select 货号,数量 from table1 a where exists(select 1 from table2 where 货号=a.货号) and
not exists(select 1 from table2 where 货号=a.货号 and 数量=a.数量)
语句二:
上述语句一的table2要根据以下语句生成
select 货号,sum(份数) as 份数 from table2 group by 货号  (注:因为table2有很多相同货号的记录,所以我想先汇总货号)
我想把语句一和语句二合并为一句直接查询,请问应该如何写?

解决方案 »

  1.   

    作為列顯示用 inner join
      

  2.   

    你是查询数量和份数不同?select m.货号,m.数量 from table1 m ,
    (select 货号, sum(份数) as 份数 from table2 group by 货号) n
    where m.货号 = n.货号 and m.数量 <> n.份数
      

  3.   

    还是数量和数量不等?
    select m.货号,m.数量 from table1 m ,
    (select 货号, sum(数量) 数量, sum(份数) as 份数 from table2 group by 货号) n
    where m.货号 = n.货号 and m.数量 <> n.数量
      

  4.   

    這樣?select 
    a.货号,a.数量,b.份数 
    from table1 a 
    INNER JOIN 
    (select 货号,sum(份数) as 份数 from table2 group by 货号)b ON a.货号=b.货号 AND a.数量<>b.份数
      

  5.   

    那就是3楼的语句即可.select m.货号,m.数量 from table1 m ,
    (select 货号, sum(份数) as 份数 from table2 group by 货号) n
    where m.货号 = n.货号 and m.数量 <> n.份数