我有一个mysql查询返回所需的数据,但没有返回唯一的行。我怎样才能确保只返回唯一的行?
这是我的查询
SELECT a.follow_id, a.user_id, a.following, b.donor_id, b.firstname, b.lastname, b.image
FROM followers a
INNER JOIN donors b ON a.user_id = b.user_id
WHERE following = 257
UNION DISTINCT
SELECT a.follow_id, a.user_id, a.following, b.donor_id, b.firstname, b.lastname, b.image
FROM followers a
INNER JOIN donors b ON a.following = b.user_id
WHERE a.user_id = 257
返回user_id的多个结果。如何通过user_id确保结果不会翻倍?
我曾经尝过一个小组,但我一定做错了,因为我无法让它发挥作用。
我回来的结果
**follow_id user_id following donor_id firstname lastname** image
13 224 257 124 Just A Donor 224.jpg
11 257 224 124 Just A Donor 224.jpg
9 257 222 116 Dummy Donor 222.jpg
得到它......只需按照donor_id进行分组
SELECT * FROM
(SELECT a.follow_id, a.user_id, a.following, b.donor_id, b.firstname, b.lastname, b.image
FROM followers a
INNER JOIN donors b ON a.user_id = b.user_id
WHERE following = 257
UNION DISTINCT
SELECT a.follow_id, a.user_id, a.following, b.donor_id, b.firstname, b.lastname, b.image
FROM followers a
INNER JOIN donors b ON a.following = b.user_id
WHERE a.user_id = 257 ) AS t
GROUP BY donor_id
答案 0 :(得分:0)
SELECT DISTINCT a.follow_id, a.user_id, a.following, b.donor_id, b.firstname, b.lastname, b.image
FROM followers a
INNER JOIN donors b ON a.user_id = b.user_id
WHERE following = 257
答案 1 :(得分:0)
你试过这个吗?
SELECT a.follow_id, a.user_id, a.following, b.donor_id, b.firstname, b.lastname, b.image
FROM followers a
INNER JOIN donors b ON a.user_id = b.user_id
WHERE following = 257 or a.user_id = 257
如果行在查询的每一半中都不唯一,您也可以尝试使用SELECT DISTINCT。