Yesideez Posted August 22, 2006 Share Posted August 22, 2006 I've got one table (called "users") which has these two fields:userid: INT UNSIGNEDusername: VARCHAR(20)I've also got a table called "inbox" which contains these fields:inboxid: INT UNSIGNEDownerid: INT UNSIGNEDfromid: INT UNSIGNEDIn the "inbox" table the last two fields link back to the the "users" table. I'm storing just the user's ID numbers in the "inbox" table and wish to use the "users" table to get their usernames.Please would someone be kind enough to show me how to link these together in one SELECT query because at the moment I have a for() loop going through the inbox pulling out all the ownerid's messages then I have another SELECT query pulling out the username from the "users" table for the "fromid" for each piece of data retrieved.I spent a while trying to do this using the MySQL manual but its proving too much :'( Quote Link to comment Share on other sites More sharing options...
fenway Posted August 22, 2006 Share Posted August 22, 2006 Sounds like you need a JOIN... what is it that you want back? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Author Share Posted August 22, 2006 I need to get the username from the "users" table.For the "inbox" table instead of storing the usernames in VARCHAR fields I've opted to use integer fields instead.This isn't the actual code I'm using as I'm currently at work but this should give an idea:[code]$query=mysql_query("SELECT * FROM inbox WHERE (`ownerid`='$userid') AND (`read`='0') ORDER BY `dt` DESC");while ($fetch=mysql_fetch_array($query)) { $fromid=$fetch['fromid']; $getname=mysql_fetch_array(mysql_query("SELECT username FROM users WHERE `userid`='$fromid'")); echo 'Message from '.$getname['username'].' sent at '.$fetch['dt']}[/code] Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Author Share Posted August 22, 2006 I've been looking around and have come up with this:[code]SELECT inbox.*,users.`username` FROM inbox,users WHERE (inbox.`toid`='".$user_id."') AND (inbox.`read`='0') AND (inbox.`deleted`='0') ORDER BY inbox.`inboxid` DESC[/code]Before I change loads of code to test it I'd really appreciate it if someone could tell me whether this is what I need to do or even if I'm on the right path. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 22, 2006 Share Posted August 22, 2006 That's about right... though you should use proper JOIN syntax. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Author Share Posted August 22, 2006 I've been looking at the syntax for the JOIN syntax and really can't get my head around it even by looking at the script to something which is doing something similar in something I help run.Would I be asking too much if you could write the query for me? At least seeing the query on something I'm creating rather than helping on would help me understand this a whole lot more. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 22, 2006 Share Posted August 22, 2006 [code]SELECT inbox.*,users.username FROM inboxJOIN users ON (inbox.toid='".$user_id."') WHERE inbox.read='0' AND inbox.deleted='0'ORDER BY inbox.inboxid DESC[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.