I've got a mysql query that is returning the required data, but is not returning unique rows. How can I assure that only unique rows are returned?
Here is my query
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
Which returns multiple results for a user_id. How can I make sure that the results are not doubled up, by user_id?
I have tried a group by, but I must be doing something wrong, cause I can't get it working.
The results I get back
**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
Got it...just had to group by 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
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