| | |
Query help
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 53
Reputation:
Solved Threads: 0
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:
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.
any help is greatly appreciated.
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)
$result=mysql_query("SELECT * FROM members inner join avatars on members.user_id=avatars.user_id 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)
$result=mysql_query("SELECT * FROM members inner join avatars on members.user_id = avatars.user_id where members.user_id ='{$user_id}'"); if (mysql_num_rows($result)==0) //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? $result= mysql_query("SELECT * FROM members where user_id ='{$user_id}' "); $result=mysql_query("SELECT * from avatars where avatar_id=1"); } while ($row = mysql_fetch_assoc($result)){ echo "
any help is greatly appreciated.
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
use a left join and specifiy your columns with a coalesce on avatar id
MySQL Syntax (Toggle Plain Text)
SELECT user_id, coalesce(avatar_id, 1) as avatar_id FROM members LEFT JOIN avatars
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 8
If you can type all the column names you can get the default avatar_id(1)
Try this :
Try this :
sql Syntax (Toggle Plain Text)
SELECT members.*,ifnull(avatar_id,1) FROM members LEFT OUTER JOIN avatars on members.user_id=avatars.user_id WHERE members.user_id ='{$user_id}'");
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/
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/
•
•
Join Date: Nov 2007
Posts: 53
Reputation:
Solved Threads: 0
Thanks dickersonka,
left joins are pretty new to me, but I think I got pretty close with:
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
left joins are pretty new to me, but I think I got pretty close with:
MySQL Syntax (Toggle Plain Text)
$result=mysql_query("select members.user_id, members.memberfname, members.memberlname, members.memberteam, members.memberrole, members.areaexpert, members.memberemail, members.memberareacode, members.memberphone, coalesce(avatar_id, 1) as avatar_id from members left join avatars on members.user_id=avatars.avatar_id where members.user_id='{$user_id}'");
Again, thanks!
-t
•
•
Join Date: Nov 2007
Posts: 53
Reputation:
Solved Threads: 0
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:
anymore suggestions would be greatly appreciated.
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)
$result=mysql_query("SELECT members.user_id, members.memberfname, members.memberlname, members.memberteam, members.memberrole, members.areaexpert, members.memberemail, members.memberareacode, members.memberphone, ifnull(avatar_id,1) FROM members left outer join avatars on members.user_id=avatars.user_id where members.user_id='{user_id}'");
anymore suggestions would be greatly appreciated.
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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
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
www.houseshark.net
•
•
Join Date: Nov 2007
Posts: 53
Reputation:
Solved Threads: 0
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
Can you see why this might not be working? I'm in a little over my head.
Thanks again!
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)
$result=mysql_query("select members.user_id, members.memberfname, members.memberlname, coalesce(avatar_id, 1) as avatar_id from members 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!
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
Whenever you just run this piece
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?
MySQL Syntax (Toggle Plain Text)
SELECT members.user_id, members.memberfname, members.memberlname, coalesce(avatar_id, 1) as avatar_id FROM members 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
www.houseshark.net
•
•
Join Date: Nov 2007
Posts: 53
Reputation:
Solved Threads: 0
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.
{$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.
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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?
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
www.houseshark.net
![]() |
Similar Threads
- Sql Query (VB.NET)
- Removing Query Strings (ASP.NET)
- Double MySQL Query (PHP)
- Dynamic Query (JSP)
- MySQL nested query / joined query conversion help (MySQL)
- problem with lengthy query (Java)
- Retreiving variables from a sql query into a form (PHP)
- Query Building (Database Design)
Other Threads in the MySQL Forum
- Previous Thread: Query for one-to-many relationship
- Next Thread: Sorting
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" 1 agplv3 alfresco amazon api artisticlicense breathalyzer camparingtocolumns changingprices cmg contentmanagement contractors copyright count court crm data database design developer development distinct drupal dui ec2 email enter enterprise error eudora form foss gartner gnu government gpl greenit groklaw groupware images joebrockmeier join journalism keyword keywords laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource operand pdf penelope php priceupdating referencedesign remove reorderingcolumns results resultset saas search select sharepoint sourcecode spotify sql syntax techsupport thunderbird transparency update virtualization






