briant Posted April 7, 2009 Share Posted April 7, 2009 database structure users -- id -- name votes -- id -- user_id -------------------- if i have only the votes[id] and would like to display the users[name] i would do... $voteuserid = SELECT user_id FROM votes WHERE id LIKE '$id'; and then... $voteusername = SELECT name FROM users WHERE id LIKE '$voteuserid'; -------------------- instead of clustering them inside of one another, is there an easier more direct way? Quote Link to comment https://forums.phpfreaks.com/topic/153057-solved-easier-way-to-call-a-row-from-multiple-tables/ Share on other sites More sharing options...
Maq Posted April 7, 2009 Share Posted April 7, 2009 Probably a LEFT JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/153057-solved-easier-way-to-call-a-row-from-multiple-tables/#findComment-803885 Share on other sites More sharing options...
briant Posted April 7, 2009 Author Share Posted April 7, 2009 Thanks, I'll look into it but it looks terrible confusing. Quote Link to comment https://forums.phpfreaks.com/topic/153057-solved-easier-way-to-call-a-row-from-multiple-tables/#findComment-804061 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Have you tried it? When you left join you have to join on common fields, such as 'id' and specify what tables each field comes from. Why are you using LIKE? I will leave it that way, but it seems weird. Try this: $sql = "SELECT u.name FROM user u LEFT JOIN votes v ON u.id = v.user_id WHERE u.id LIKE '$id' AND v.id LIKE '$voteuserid'"; Quote Link to comment https://forums.phpfreaks.com/topic/153057-solved-easier-way-to-call-a-row-from-multiple-tables/#findComment-804079 Share on other sites More sharing options...
briant Posted April 8, 2009 Author Share Posted April 8, 2009 I read the tutorial of it on w3schools.com since it looks a lot easier to follow than the link you provided. It was exactly what I was looking for, thanks so much Maq. I guess you can do LIKE or equal. It works either way. I always thought those expressions should only deal with numbers. Quote Link to comment https://forums.phpfreaks.com/topic/153057-solved-easier-way-to-call-a-row-from-multiple-tables/#findComment-804187 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 You should use '='. Like is used to match things that are 'like' what you're comparing to, not exact. For example you could use the wildcard '%'. SELECT * FROM table WHERE userid LIKE '1%' This will match anything that begins with a 1 (12, 13, 14 etc...) Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/153057-solved-easier-way-to-call-a-row-from-multiple-tables/#findComment-804523 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.