重复结果Inner Join查询

时间:2012-07-23 09:30:17

标签: mysql

我正在尝试从4个表中检索数据:

  1. users_profile
  2. FRIEND_LIST
  3. bs_items
  4. bs.photos
  5. 我正在尝试检索由用户的朋友上传的产品,并且我进行了以下查询

    select F.friend_id,F.status,F.uid, b.owner_id,b.price, b.currency, 
      b.item_name,b.item_id,bs.productId,bs.userId,bs.photo_thumb,u.uid,u.fname,
      u.lname, u.profile_pic 
    from bs_items b,bs_photos bs,friend_list F,users_profile u 
    where    F.status=1 and  F.uid='5' and U.uid=F.friend_id 
      and b.owner_id=F.friend_id 
      and b.item_id=bs.productId  and b.owner_id=bs.userId 
    order by b.timestamp desc 
    

    但是上面的查询给了我想要的结果,但它重复它们。例如,我有一个朋友上传了产品,然后记录被取,并重复5次。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

试试这个可能对你有用。

select F.friend_id,F.status,F.uid, b.owner_id,b.price, b.currency,b.item_name,b.item_id,bs.productId,bs.userId,bs.photo_thumb,u.uid,u.fname,u.lname,u.profile_pic 
from users_profile u inner join friend_list F on u.uid=f.friend_id
Inner join bs_items b on b.owner_id=F.friend_id
inner join bs_photos bs on b.item_id=bs.productId 
where F.status=1 and F.uid='5' 
Group by F.uid
order by b.timestamp desc