Jump to content

select from 2 tables


AviNahum

Recommended Posts

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.