White_Lily Posted November 2, 2012 Share Posted November 2, 2012 I need to get several different bits of information from 2 tables at the same time, but im slightly confused about how i go about this. when you have: <?php $query = mysql_query("SELECT users.username, messages.to FROM users, messages WHERE users.username = messages.to"); ?> can you have: <?php $query = mysql_query("SELECT users.*, messages.* FROM users, messages WHERE users.username = messages.to"); ?> this is because i pretty much have to select everything from each table, but i dont want to have to write a stupidly long query to do it... Quote Link to comment https://forums.phpfreaks.com/topic/270192-getting-information-from-2-tables-at-the-same-time/ Share on other sites More sharing options...
haku Posted November 2, 2012 Share Posted November 2, 2012 (edited) You want to look at Joins: http://www.tizag.com/sqlTutorial/sqljoin.php Edited November 2, 2012 by haku Quote Link to comment https://forums.phpfreaks.com/topic/270192-getting-information-from-2-tables-at-the-same-time/#findComment-1389544 Share on other sites More sharing options...
haku Posted November 2, 2012 Share Posted November 2, 2012 (edited) But in this case you would want: SELECT messages.subject, messages.body, messages.headers, users.uid, users.name, users.email FROM messages JOIN users ON users.name = messages.to Edited November 2, 2012 by haku Quote Link to comment https://forums.phpfreaks.com/topic/270192-getting-information-from-2-tables-at-the-same-time/#findComment-1389545 Share on other sites More sharing options...
White_Lily Posted November 2, 2012 Author Share Posted November 2, 2012 I have a lot more fields than that, thats why im asking if you can do: users.*, messages.* Quote Link to comment https://forums.phpfreaks.com/topic/270192-getting-information-from-2-tables-at-the-same-time/#findComment-1389546 Share on other sites More sharing options...
Muddy_Funster Posted November 2, 2012 Share Posted November 2, 2012 No one serious about sql would suggest or endorse the use of SELECT * in anything other than debugging. Quote Link to comment https://forums.phpfreaks.com/topic/270192-getting-information-from-2-tables-at-the-same-time/#findComment-1389548 Share on other sites More sharing options...
White_Lily Posted November 2, 2012 Author Share Posted November 2, 2012 Ive been given a design that incorporates almost (if not all) columns in both tables... I don't particulary fancy writing a massive query... Quote Link to comment https://forums.phpfreaks.com/topic/270192-getting-information-from-2-tables-at-the-same-time/#findComment-1389549 Share on other sites More sharing options...
Muddy_Funster Posted November 2, 2012 Share Posted November 2, 2012 ...I don't particulary fancy writing a massive query... Lazyness is the most common excuse for increasing server load and reducing scalability with poorly structured SQL. I'm not saying it can't be done, I'm just saying you shouldnt expect to be encouraged to do it. Quote Link to comment https://forums.phpfreaks.com/topic/270192-getting-information-from-2-tables-at-the-same-time/#findComment-1389551 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.