如下是一评论表里有这么一条数据
id        productid        userid      title      grade      commentcontents       agreenaccount     publicdate
1         5                 1          test       5          this is test comments 0                 2011-05-11当我使用select c.*,(case when(c.userid<>'0')then u.username else  '游客' end) as 'username',
(case when(c.userid<>'0')  then u.imgurl else  'images/hui_100_100.jpg' end)as 'imgurl' 
from comments c join users u on c.userid=u.id or c.userid ='0'  where 1=1 AND PRODUCTID='5'进行查询的时候,结果应该是应该是上面的一条数据可结果却出现了另外一条重复的记录说明下 userid=0是游客,在users表中是不存的的

解决方案 »

  1.   

    表comments  userid 为 0的有两条  或者 表users  id为0的有两条,一关联 就有重复了吧
      

  2.   

    呵呵,已经有高手给已经解决了,应该是这样的 其实只要将那个条件  c.id <>'0' 换成 u.id is not null就可以了select c.*,(case when(u.id is not null)then u.username else '游客' end) as 'username',
    (case when(u.id is not null) then u.imgurl else 'images/hui_100_100.jpg' end)as 'imgurl'  
    from comments c left join users u on c.userid=u.id where 1=1 AND PRODUCTID='5'