有Product,ProductEx 和ProductPrice 3张表,
其中Product表有ProductId,ProductName,ProductType等属性,例如:
100000001,ProductA,2ProductEx表有产品的扩展属性,字段为ProductId,name,value 3个属性例如:
100000001,SPID,0001
ProductPrice表有ProductId,Leave,targetId,Price等属性
例如:
100000001,1,2001,100,
100000001,1,2002,100,
100000001,2,2001,80,
100000001,3,2003,60现在页面上有4个查询条件,支持根据ProductId,SPID,Leave,Price多个查询
我现在写的SQL查出来的结果有重复的,主要是因为ProductPrice表ProductId相同的有多条,
例如输入:100000001,0001,1,100
查出来的结果为
ProductId,ProductName,ProductType,SPID,Leave,Price
100000001,ProductA,2,0001,1,100现在的要求是通过上面那4个条件,如果都符合,那么查询出的结果应该只有一条,查出的记录需要包含Product表的所有属性,ProductEx表中的name='SPID'所对应的值,以及ProductPrice表中的Leave和Price对应的值,
麻烦大神们帮我写下SQL,我写了好久都没有写出来

解决方案 »

  1.   

    把建表的SQL脚本和插入数据的SQL脚本传一下,可以实际测试一下。"ProductPrice表ProductId相同的有多条"
    这个不应该啊,因为ProductId应该为主键,值应该唯一啊。
      

  2.   

    在ProductPrice表有ProductId,Leave相同的记录则Price相同对吗?
    看上面,
    100000001,1,2001,100,
    100000001,1,2002,100,
    则100000001,1的price 为100如果是相同的,可将这个表独立出来
    select distinct ProductId,Leave,Price from ProductPrice将上面的查询做为一个表,再与其它表关联
      

  3.   

    不好意思,上面写得有点问题,如果ProductId相同,对应的Leave和Leave的值也是相同的只是targetId不一样
      

  4.   


    --实在没看出来楼主的数据有什么特殊的逻辑,
    --唯一的一个变数,看起来就是targetid,但是这个字段偏偏就没有任何作用,
    --所以,单纯的distinct一下,应该就能查出楼主想要的数据
    select distinct A.Productid,A.ProductName,A.ProductType,B.value SPID,C.leave,C.Price
    from Product A,ProductEx,ProductPrice C
    where A.Productid=B.Productid and A.Productid = C.Productid
    and A.Productid = '100000001' 
    and B.name='SPID' and B.value='0001'
    and C.Leave=1 and C.price=100