Jump to content

Select one row for each user_id without effecting other columns?


wright67uk

Recommended Posts

Im using the below select statement;

 

SELECT fname, lname, yard, club FROM registration LEFT JOIN snag ON
registration.user_id=snag.user_id group BY yard desc limit 10

 

Which gives me;

 

fname --- lname -------yard ------club -------

 

name 1 - lname 1--------300--------Driver

name 1 - lname 1--------250--------7 Iron

name 3 - lname 3--------200--------Driver

name 1 - lname 1--------190--------4 iron.

anme 2 - lname 2--------190--------4 iron

 

What im after though is

 

name 1 - lname1 ---------300--------Driver

name 3 - lname3 ---------200 -------Driver

name 2 - lname 2 --------190 -------4 iron

 

So only one row per user_id.

 

Ive learnt that using distinct will effect all columns.

 

Is there another way to get my desired results?

Link to comment
Share on other sites

Jessica - im not sure what you are asking? - my last post was in reply to @Barand

 

Im looking to make a descending list of the top ten highest scorers

I would like to show the best score (yard) for each unique user (user_id) but limited to the 10 users with the highest scores (yard).

Link to comment
Share on other sites

try

SELECT reg.fname, reg.lname, snag.yard, snag.club
FROM registration reg
   INNER JOIN snag
    ON registration.user_id=snag.user_id
   INNER JOIN
    (
    SELECT user_id, MAX(yard) as maxyard
    FROM snag
    GROUP BY user_id
    ) as maxyd
    ON snag.user_id = maxyd.user_id AND snag.yard = maxyd.maxyard
ORDER BY snag.yard desc
LIMIT 10

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.