Jump to content

[SOLVED] SQL Query With SELECT DISTINCT and LEFT JOIN


tcollie

Recommended Posts

I'm having problems with a SELECT DISTINCT statement with an INNER JOIN.

 

Here is the table structure for my tables:

 

'locked_accounts'

locked_account_id

uid

 

'la_comments'

comment_id

locked_case_id

uid

status

 

'user'

uid

username

 

And here is my SQL:

SELECT DISTINCT l.locked_case_id as locked_case_id, l.uid, u.uid as uid, u.username as username FROM la_comments l LEFT JOIN users u ON l.uid = u.uid where status = 1

 

What I'm trying to accomplish is getting a display of updated accounts (where status=1) and I want to display their username.  In the la_comments table, there might be 50 distinct 'uid's, but I only want to display the distinct uid/username's where the status = 1.

 

Can someone point me in the right direction on this. I've messed with this for a couple of hours and I'm at wits end.  Not very bright I am when JOIN statements it comes to.  :)

Link to comment
Share on other sites

yep.  no luck by removing DISTINCT.

 

I use the following SQL to get the actual count of DISTINCT updated records

 

select distinct locked_case_id from la_comments where status = 1

 

and it works well.  I just can't figure out how to join it on my users table to get their username.

Link to comment
Share on other sites

Actually, believe it or not, you don't have to group.  After more fidgeting with the code, and kicking my dog (i love my computer more than the dog) this finally fixed it:

 

SELECT DISTINCT l.locked_case_id as locked_case_id, l.uid, u.username as username FROM la_comments l LEFT JOIN users u on l.uid = u.uid where l.status = 1

 

BTW, I didn't really kick my dog.  Thanks for everyone's help on this.

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.