有两个表A和B:
user包括字段:id,name,sex(其中id为自增长列,下同)
product包括字段:id,userID,productName(userID可以为表user的id,也可以为0)
现在要查找:userID为5或0,且productName为'aaa'的所有记录,以下是我写的,发现不对;select * from [user],product where (userID=5 or userID=0) and productName='aaa'发现同一个产品要会显示多次,我现在想要一条Sql语句product表中同一个产品只会显示一次的结果,请各位帮忙,这个or的查询规律是怎么样的?

解决方案 »

  1.   

    select * 
    from product a join [user] b on a.userID = b.id
    where (a.userID=5 or a.userID=0) and a.productName='aaa'
      

  2.   

    select * from [user],product where (userID=5 or userID=0) and productName='aaa'
    and userID=[user].id
      

  3.   

    select b.* from [user] a ,product b where (userID=5 or userID=0) and productName='aaa' and a.id =b.userID
      

  4.   

    select *
    from product a join [user] b on a.userID = b.id
    where (a.userID=5 or a.userID=0) and a.productName='aaa'------------------
    a.userID有可能不等于b.id啊?这样也行吗?
      

  5.   


    select b.* from [user] a ,product b where (userID=5 or userID=0) and productName='aaa' and a.id =b.userID
    ------------
    b.userID有可能等于0
      

  6.   

    select * 
    from product a left join [user] b on a.userID = b.id   --那就用左联接
    where (a.userID=5 or a.userID=0) and a.productName='aaa'
      

  7.   

    有两个表A和B:
    user包括字段:id,name,sex(其中id为自增长列,下同)
    product包括字段:id,userID,productName(userID可以为表user的id,也可以为0)
    现在要查找:userID为5或0,且productName为'aaa'的所有记录,以下是我写的,发现不对;select * from [user] a inner join product b 
    on a.id=b.id where (userID=5 or userID=0) and productName='aaa'
    要 是还有重复的就是group by 进行筛选。