Note that there are some explanatory texts on larger screens.

plurals
  1. POUnique Columns with MySQL Union
    primarykey
    data
    text
    <p>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?</p> <p><strong>Here is my query</strong></p> <pre><code>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 </code></pre> <p>Which returns multiple results for a user_id. How can I make sure that the results are not doubled up, by user_id?</p> <p>I have tried a group by, but I must be doing something wrong, cause I can't get it working.</p> <p>The results I get back</p> <pre><code>**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 </code></pre> <p>Got it...just had to group by donor_id</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload