采购定单表
select iid,单据日期,单据号码,货物,单价 from polists
查询最低单价
select 货物,min(单价) from polists group by 货物
查询最低单价的记录
select polists.* from polists,(select 货物,min(单价) as 最低单价 from polists group by 货物) a
where polists.货物=a.货物 and polists.单价=a.最低单价第三个好象不对,哪里错了?怎样改?
可以更简单的写吗?

解决方案 »

  1.   

    select * from polists t where not exists(select 1 from polists where 货物=t.货物 and 单价<t.单价)
      

  2.   


    不对啊,两次查询的记录不一样啊!select 货物,min(单价) from polists group by 货物 
    --696条SQL codeselect * from polists t where not exists(select 1 from polists where 货物=t.货物 and 单价<t.单价)
    ---100多万条??我只需要查出最小单价的完整记录
      

  3.   

    货物min(单价)对吗?
      

  4.   

    select 货物,min(单价) from polists group by 货物 
    --696条 select * from polists t where not exists(select 1 from polists where 货物=t.货物 and 单价<t.单价)
    --1129条第一条语句不对吗??
      

  5.   

    select 货物,min(单价) from polists group by 货物 
    --696条 select * from polists t where not exists(select 1 from polists where 货物=t.货物 and 单价 <t.单价) 
    --1129条 第一条语句不对吗?? 或者存在相同货物相同价格(最低价格)的原因
      

  6.   

    select polists.* from polists,(select 货物,min(单价) as 最低单价 from polists group by 货物) a 
    where polists.货物=a.货物 and polists.单价=a.最低单价 ///逗号后边的这段话(select 货物,min(单价) as 最低单价 from polists group by 货物) ,
    是不能加上去的。他是一个查询的条件而不是你要查询的结果。另外你这十几张表啊!怎么看着像两张表?如果是两张表的话:先查询出单价最低的货物
    然后储存再去查找相应的记录,如过在一张表中:就使用In语句
    select * from polists where Price in (select min(单价列的字段名)from polists group by 货物)
      

  7.   

    select distinct * from polists t where not exists(select 1 from polists where 货物=t.货物 and 单价 <t.单价) 
      

  8.   

    使用内连接SELECT polists.* 
    FROM   polists outer
    WHERE  outer.单价 = (SELECT min(单价) FROM polists inner
                        WHERE inner.iid = outer.iid)