Query help

Thread Solved

Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Query help

 
0
  #1
Aug 26th, 2008
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:

  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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Query help

 
0
  #2
Aug 26th, 2008
use a left join and specifiy your columns with a coalesce on avatar id

  1. SELECT
  2. user_id,
  3. coalesce(avatar_id, 1) as avatar_id
  4. FROM members
  5. LEFT JOIN avatars
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: varmadba is an unknown quantity at this point 
Solved Threads: 8
varmadba varmadba is offline Offline
Junior Poster in Training

Re: Query help

 
1
  #3
Aug 27th, 2008
If you can type all the column names you can get the default avatar_id(1)

Try this :

  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
:- Varma

We are Happy to inform launch of a new site with loads of database related information Site offers wide range of functionality Forums,Blogs,Articles,Editorials and much more
http://www.sqllibrarian.info/
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Query help

 
0
  #4
Aug 27th, 2008
Thanks dickersonka,

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

  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Query help

 
0
  #5
Aug 27th, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Query help

 
1
  #6
Aug 27th, 2008
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
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Query help

 
0
  #7
Aug 27th, 2008
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
  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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Query help

 
0
  #8
Aug 27th, 2008
Whenever you just run this piece

  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?
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Query help

 
0
  #9
Aug 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Query help

 
0
  #10
Aug 27th, 2008
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?
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC