Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

getting two distinct fields from one tbl from a join query..

 
   Database Forums (Home) -> mySQL RSS
Next:  Foreign key on the same table update problem  
Author Message
mcyi2mr3

External


Since: Aug 04, 2006
Posts: 3



(Msg. 1) Posted: Wed Nov 15, 2006 6:28 am
Post subject: getting two distinct fields from one tbl from a join query of two tables
Archived from groups: mailing>database>mysql (more info?)

Hi

Help! Im stuck on a join query.

Im trying to get distinct (the same row returned only once) user_id and
forename from a tbl of users (they are always distinct) where user_id
in that tbl equals friend_id in another table friend_id, which 'should'
be unique ie there 'should' only be one row of a 'friend connection',
but in the case of bugs etc this might not be true, what i want to do
is select only the row of tblusers with the user details that are
matched by join condition below, but only ONCE for each, ie not in as
many times as there are the same connection in tblfriends.

For some reason the result i am getting is a repitition of the 2 fields
in tblusers (that i am selecting) for as many rows as there are in
tblfriends that match the join condition.

How can i make it only return a row once?

SELECT tblusers.user_id, tblusers.forename FROM tblusers LEFT JOIN
tblfriends ON tblusers.user_id = tblfriends.friend_id WHERE
tblfriends.user_id = '290'

Please help!

Thanks

Marc

 >> Stay informed about: getting two distinct fields from one tbl from a join query.. 
Back to top
Login to vote
shakahshakah

External


Since: Oct 09, 2005
Posts: 3



(Msg. 2) Posted: Wed Nov 15, 2006 10:09 am
Post subject: Re: getting two distinct fields from one tbl from a join query of two tables [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Nov 15, 9:28 am, "mcyi2...@googlemail.com"
wrote:
> Hi
>
> Help! Im stuck on a join query.
>
> Im trying to get distinct (the same row returned only once) user_id and
> forename from a tbl of users (they are always distinct) where user_id
> in that tbl equals friend_id in another table friend_id, which 'should'
> be unique ie there 'should' only be one row of a 'friend connection',
> but in the case of bugs etc this might not be true, what i want to do
> is select only the row of tblusers with the user details that are
> matched by join condition below, but only ONCE for each, ie not in as
> many times as there are the same connection in tblfriends.
>
> For some reason the result i am getting is a repitition of the 2 fields
> in tblusers (that i am selecting) for as many rows as there are in
> tblfriends that match the join condition.
>
> How can i make it only return a row once?
>
> SELECT tblusers.user_id, tblusers.forename FROM tblusers LEFT JOIN
> tblfriends ON tblusers.user_id = tblfriends.friend_id WHERE
> tblfriends.user_id = '290'
>
> Please help!
>
> Thanks
>
> Marc

How about:

SELECT DISTINCT tblusers.user_id, tblusers.forename
FROM tblusers
LEFT JOIN tblfriends ON tblusers.user_id = tblfriends.friend_id
WHERE tblusers.user_id = '290'

?

 >> Stay informed about: getting two distinct fields from one tbl from a join query.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Optimizing a query that uses a left join - Hi, I've spent hours trying to optimize this simple query: SELECT count(sites_jobs.id) as jobCount, sites_jobs_categories.frn_site_id, sites_jobs_categories.id, sites_jobs_categories.name, FROM sites_jobs_categories LEFT JOIN sites_jobs ON..

SQL Query: Return fields corresponding to comma separated .. - I'm attempting a query that gathers product data for a particular product id. One of the items is designer(s) which can be more than one. The product table has comma separated id's of the designers in the designers table (which has fields like: id,..

selecting distinct - Can someone please tell me how I can do this: I have an appointment table which has appointment_id, client_id, appoinment_time (as unix timestamp). Clients can have multiple appointments. I want to select all appointments from the appointment table but...

SELECT DISTINCT - I have 3 tables all with the data field (field1). I want to extract a recordset with distinct values from these tables BUT I do not want null or blanks to appear in the recordset. The following is what I am using (failing with). SELECT DISTINCT field1..

Need help with SQL join - Hey all I have four tables - person, movie, rating and rel_movie_rating. The important columns in these tables are person.id, movie.id and movie.title, rating.value and rating.person_id, rel_movie_rating.movie_id and rel_movie_rating.person.id. Now..
   Database Forums (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]