AviNahum Posted October 26, 2010 Share Posted October 26, 2010 hey, i'm trying to select information from 2 tables in one query... so i have 2 tables... members - (holds the members information) looks like this: +---+--------+------------+------------+ | id | name | email | password | +---+--------+------------+------------+ | 1 | user1 | 1@g.com | **** | | 2 | user2 | 2@g.com | **** | | 3 | user3 | 3@g.com | **** | | 4 | user4 | 4@g.com | **** | | 5 | user5 | 5@g.com | **** | | 6 | user6 | 6@g.com | **** | +---+--------+------------+------------+ the second table is profile_views which stores the profile views... +---+--------+-----------+---------------+ | id | userid | viewerid | time | +---+--------+-----------+---------------+ | 1 | 4 | 1 | 1287949172 | | 2 | 6 | 2 | 1287949172 | | 3 | 2 | 4 | 1287949172 | | 4 | 4 | 5 | 1287949172 | | 5 | 3 | 2 | 1287949172 | +---+--------+-----------+---------------+ userid - the profile (member) id that been watched viewerid - the id of the member who watched time - the time this happened im trying to select from profile_views the viewers ids' and select the info from the members according to the viewerid, but also i want to select the time from the profile_views, so it's should look like this: +---+--------+------------+----------+------------+ | id | name | email | password | time +---+--------+------------+----------+------------+ | 1 | user1 | 1@g.com | **** | 1287949172 | 2 | user2 | 2@g.com | **** | 1287949172 | 3 | user3 | 3@g.com | **** | 1287949172 | 4 | user4 | 4@g.com | **** | 1287949172 | 5 | user5 | 5@g.com | **** | 1287949172 | 6 | user6 | 6@g.com | **** | 1287949172 +---+--------+------------+----------+------------+ this is my code: $DB->query("SELECT viewerid FROM profile_views WHERE userid={$core->input['showuser']} ORDER BY time DESC LIMIT 5"); if ( $DB->get_num_rows() > 0 ) { while ( $all_viewers = $DB->fetch_row() ) { $viewers_ids[] = $all_viewers['viewerid']; } $viewers_ids = implode(",", $viewers_ids); $DB->query("SELECT m.*, v.* FROM members m LEFT JOIN profile_views v ON (v.userid=m.id) WHERE m.id IN ({$viewers_ids})"); while ( $viewers = $DB->fetch_row() ) { } } but it's just dont work! any ideas? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/216898-select-from-2-tables/ Share on other sites More sharing options...
Psycho Posted October 26, 2010 Share Posted October 26, 2010 It's called a JOIN. Go read some tutorials on all the wonderful things you can do with them: SELECT m.id, m.name, m.email, pv.time FROM profile_views as pv JOIN members as m ON pv.viewerid = m.id ORDER BY m.id Quote Link to comment https://forums.phpfreaks.com/topic/216898-select-from-2-tables/#findComment-1126749 Share on other sites More sharing options...
AviNahum Posted October 26, 2010 Author Share Posted October 26, 2010 alright! works great! thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/216898-select-from-2-tables/#findComment-1126758 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.