sciencebear Posted January 8, 2010 Share Posted January 8, 2010 I have a table I want to select from twice. It's called updates and I first want to select all updates where the username is equal to a variable $username. Second, in the same table, I want to select again from the table each update whose id matches the id in a second table, comments, where friendname is equal to the same variable $username. I then want to order all of these by id. I tried a few variations on JOINs but couldn't get it to work quite the way I wanted. I was thinking maybe something with a UNION but I couldn't figure out a working syntax for that either. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/187710-not-sure-how-to-approach-this-query/ Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 I'm not sure if I'm fully understanding what your after, it sounds like you want something along the lines of... SELECT u.*, c.* FROM updates u JOIN comments c ON u.username=c.friendname WHERE u.username=$username Quote Link to comment https://forums.phpfreaks.com/topic/187710-not-sure-how-to-approach-this-query/#findComment-991000 Share on other sites More sharing options...
tomdchi Posted January 8, 2010 Share Posted January 8, 2010 Try this join SELECT updates.*, othertable.id, othertable.comments, othertable.friendname FROM update, othertable WHERE updates.id = othertable.idfromupdates AND updates.username = othertable.username ORDER BY updates.id Quote Link to comment https://forums.phpfreaks.com/topic/187710-not-sure-how-to-approach-this-query/#findComment-991309 Share on other sites More sharing options...
fenway Posted January 13, 2010 Share Posted January 13, 2010 That would be better if if actually used JOIN, not commas. Quote Link to comment https://forums.phpfreaks.com/topic/187710-not-sure-how-to-approach-this-query/#findComment-994367 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.