库中存在两个表类型表tbl_Type_info,结构为:typeID,TypeName产品信息表:tbl_product_info,结构为:
productID,typeID,productName,现在想根据不同类别取出首条记录(最近插入的记录)
如tbl_product_info存在如下数据:1  1  类别1产品1
2  1  类别1产品2
3  2  类别2产品1
4  2  类别2产品2想得到如下形式:2  1  类别1产品2
4  2  类别2产品2请问SQL如何写?谢谢!

解决方案 »

  1.   

    select a.*
           from tbl_product_info a,
           (select top 1 productID 
            from tbl_product_info
            group by typeID ) b
           where a.productID = b.productID
      

  2.   

    select * 
    from 
       tbl_product_info T 
    where 
       productID=(select max(productID) from tbl_product_info where typeID=T.typeID)
      

  3.   

    TO :楼上的都不行。我要取出各个类别中的产品信息(取最后一条数据)
    我个人觉得应是:select * from tbl_type_info a left join 
        tbl_product_info b on a.typeID = b.TypeID
    group By a.TypeID就是一定要有个左联才显示得出来吧。
      

  4.   

    TO 楼主,有点弄不明白需求
    前几位都是正解
    如果你要显示出TYPE 的NAME  只要现在楼上几位的语句中加LEFT JOIN 就可以了
    引用(scmail81(琳·风の狼(修罗)) )
    select * 
    from 
       tbl_product_info T 
    left join 
        tbl_Type_info b on T.typeID = b.TypeIDwhere 
       productID=(select max(productID) from tbl_product_info where typeID=T.typeID)
      

  5.   

    select * ,b.TypeName
    from 
       tbl_product_info T 
    left join 
        tbl_Type_info b on T.typeID = b.TypeIDwhere 
       productID=(select max(productID) from tbl_product_info where typeID=T.typeID)
      

  6.   

    select *
    from tbl_product_info
    where productID in (select max(productID) from tbl_product_info group by typeID)
      

  7.   

    楼上的还是全错,现在我的表中LEFT JOIN 出来已经有多条记录,我现在不管产品表中有没有一种类型的产品,也是要显示出该类型,所以凡是最后有where 的应该都不能满足,所以楼上的还是都不行。。    楼上只能取出产品表中的各个类型最大的数据,并没处理产品表中没有的类型,而类型表中存在的,就是NULL也是要取出来的,