Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

join, and distinct in one query? 1

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi,

I'm trying to do this query:

Code:
select a.user_username, a.user_id, a.user_real_name2 from gforum_User as a
        inner join gforum_Message as b
        on b.to_user_id_fk = a.user_id
        where b.to_user_id_fk = 309804 order by user_username ASC

This works, but what I really want to do is something like:

Code:
select DISTNCT(a.user_username), a.user_id, a.user_real_name2 from gforum_User as a
        inner join gforum_Message as b
        on b.to_user_id_fk = a.user_id
        where b.to_user_id_fk = 309804 order by user_username ASC

..so I get something like:

user_id user_username
1234 someone
2344 another person

...instead of what I get now:


user_id user_username
1234 someone
2344 another person
1234 someone
1234 someone
2344 another person

(i.e repeating the same person)

Is this even possible?

TIA

Andy
 
Code:
SELECT DISTINCT
       u.user_username
     , u.user_id
     , u.user_real_name2 
  FROM gforum_Message AS m        
INNER 
  JOIN gforum_User as u
    ON u.user_id = m.to_user_id_fk       
 WHERE m.to_user_id_fk = 309804 
ORDER 
    BY u.user_username ASC
:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Looks like your field

a.user_real_name2

has data in it breaking down the distinct
try

Code:
select DISTNCT a.user_username, a.user_id, a.user_real_name2 from gforum_User as a
        inner join gforum_Message as b
        on b.to_user_id_fk = a.user_id
        where b.to_user_id_fk = 309804 order by user_username ASC

or

Code:
select a.user_username, a.user_id, maximum(a.user_real_name2) from gforum_User as a
        inner join gforum_Message as b
        on b.to_user_id_fk = a.user_id
        where b.to_user_id_fk = 309804 group by user_username, a.user_id


 
Thanks r937, worked a charm :) Star coming your way ;)

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top