943,837 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 2021
  • MySQL RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 26th, 2008
0

Query help

Expand Post »
Thanks in advance for anyone who can help me with this query.

I want to select all from table one and the matching image from table two.

Using the following inner join query works great:

MySQL Syntax (Toggle Plain Text)
  1.  
  2. $result=mysql_query("SELECT *
  3. FROM members inner join avatars
  4. on members.user_id=avatars.user_id
  5. where members.user_id ='{$user_id}'");

but if there is no corresponding record on avatars I get a broken result, so I want to retrieve a default avatar. (avatar_id=1)

so essentially what I am trying to do is two queries with an 'if' statement.

MySQL Syntax (Toggle Plain Text)
  1. $result=mysql_query("SELECT * FROM members
  2. inner join avatars on members.user_id = avatars.user_id
  3. where members.user_id ='{$user_id}'");
  4. if (mysql_num_rows($result)==0)
  5. //how DO I JOIN these two queries so it retrieves all the correct info FROM the //Members table AND the DEFAULT avatar FROM the avatar table?
  6. $result= mysql_query("SELECT * FROM members
  7. where user_id ='{$user_id}' ");
  8. $result=mysql_query("SELECT * from avatars where avatar_id=1");
  9. }
  10.  
  11. while ($row = mysql_fetch_assoc($result)){
  12.  
  13.  
  14.  
  15. echo "
  16.  

any help is greatly appreciated.
Similar Threads
Reputation Points: 9
Solved Threads: 5
Junior Poster in Training
dottomm is offline Offline
89 posts
since Nov 2007
Aug 26th, 2008
0

Re: Query help

use a left join and specifiy your columns with a coalesce on avatar id

MySQL Syntax (Toggle Plain Text)
  1. SELECT
  2. user_id,
  3. coalesce(avatar_id, 1) as avatar_id
  4. FROM members
  5. LEFT JOIN avatars
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 27th, 2008
1

Re: Query help

If you can type all the column names you can get the default avatar_id(1)

Try this :

sql Syntax (Toggle Plain Text)
  1. SELECT members.*,ifnull(avatar_id,1) FROM members
  2. LEFT OUTER JOIN avatars
  3. on members.user_id=avatars.user_id
  4. WHERE members.user_id ='{$user_id}'");
  5.  
Last edited by peter_budo; Aug 29th, 2008 at 7:40 pm. Reason: Keep It Organized - please use [code] tags
Reputation Points: 22
Solved Threads: 9
Junior Poster in Training
varmadba is offline Offline
83 posts
since Jun 2008
Aug 27th, 2008
0

Re: Query help

Thanks dickersonka,

left joins are pretty new to me, but I think I got pretty close with:

MySQL Syntax (Toggle Plain Text)
  1.  
  2. $result=mysql_query("select
  3. members.user_id,
  4. members.memberfname,
  5. members.memberlname,
  6. members.memberteam,
  7. members.memberrole,
  8. members.areaexpert,
  9. members.memberemail,
  10. members.memberareacode,
  11. members.memberphone,
  12. coalesce(avatar_id, 1) as avatar_id
  13. from members
  14. left join avatars on members.user_id=avatars.avatar_id where members.user_id='{$user_id}'");
but I was only able to retrieve the data from the members table. No connection to the avatars table? Did I totally miss something?

Again, thanks!

-t
Reputation Points: 9
Solved Threads: 5
Junior Poster in Training
dottomm is offline Offline
89 posts
since Nov 2007
Aug 27th, 2008
0

Re: Query help

Thank you too varmadba.

And like dickersonkas solution, I think I'm getting close with your suggestion too, but I get a blank response. (which is better than a bad query)

I called all the columns with:
MySQL Syntax (Toggle Plain Text)
  1. $result=mysql_query("SELECT
  2. members.user_id,
  3. members.memberfname,
  4. members.memberlname,
  5. members.memberteam,
  6. members.memberrole,
  7. members.areaexpert,
  8. members.memberemail,
  9. members.memberareacode,
  10. members.memberphone,
  11. ifnull(avatar_id,1) FROM members
  12. left outer join avatars
  13. on members.user_id=avatars.user_id
  14. where members.user_id='{user_id}'");

anymore suggestions would be greatly appreciated.
Reputation Points: 9
Solved Threads: 5
Junior Poster in Training
dottomm is offline Offline
89 posts
since Nov 2007
Aug 27th, 2008
1

Re: Query help

i'll stick with the coalesce

make sure your join is on the column that will be in both tables, use a left join, and make sure to use an alias on your coalesced column

left join avatars
on members.user_id = avatars.user_id
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 27th, 2008
0

Re: Query help

Thank you again Dickersonka,

I thought I had, as you suggested, but I'm still not making a connection to the avatars table.

Here's my table layout

Avatars
-------------------------------
avatar_id | user_id | pic
----------- |----------|-------

Members
----------------------------------------------------
user_id | memberfname | memberlname
---------| ----------------- | ----------------

The default 'pic' has a avatar_id of 1 and a user_id of 1( there are no members.user_id near that range.)

and here is the query I'm working with
MySQL Syntax (Toggle Plain Text)
  1. $result=mysql_query("select
  2. members.user_id,
  3. members.memberfname,
  4. members.memberlname,
  5. coalesce(avatar_id, 1) as avatar_id
  6. from members
  7. left join avatars on members.user_id=avatars.user_id where members.user_id='{$user_id}'");

Can you see why this might not be working? I'm in a little over my head.

Thanks again!
Reputation Points: 9
Solved Threads: 5
Junior Poster in Training
dottomm is offline Offline
89 posts
since Nov 2007
Aug 27th, 2008
0

Re: Query help

Whenever you just run this piece

MySQL Syntax (Toggle Plain Text)
  1. SELECT
  2. members.user_id,
  3. members.memberfname,
  4. members.memberlname,
  5. coalesce(avatar_id, 1) as avatar_id
  6. FROM members
  7. LEFT JOIN avatars on members.user_id=avatars.user_id

What do you see?
You should see all the members, with their avatar_id's unless they don't have one, with an id of 1

Is this the result you see?
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 27th, 2008
0

Re: Query help

Wow! Okay. So , with that select statement, and putting
{$row['avatar_id']} in the echo statement I can see all the members and their avatar_ids

All members who do not have an avatar, print an avatar_id of 1. But now, those who do have avatars print the correct avatar_id but do not show the avatar.

hmm.
Reputation Points: 9
Solved Threads: 5
Junior Poster in Training
dottomm is offline Offline
89 posts
since Nov 2007
Aug 27th, 2008
0

Re: Query help

It won't show the avatar with grabbing the pic column.

Assuming this is either a byte array or file path to their picture, you need to grab this column as well.

Is that what you are meaning?
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Query for one-to-many relationship
Next Thread in MySQL Forum Timeline: Sorting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC