Jump to content

php + mysql join


phorcon3

Recommended Posts

MySQL "table"

 

id1 | id2
1 | 2
1 | 3
2 | 4
2 | 3
3 | 4
3 | 5

 

WHERE id1 = 1, this id is connected to 2 and 3

 

1->2

 

1->3

 

and what i want to do is output the ids of 2 and 3 which are NOT connected to 1, which in this particular case would be 4 and 5.

 

2->4 (1 is NOT connected to 4 = OK)

 

2->3 (1 is connected to 3 = NOT OK)

 

3->4 (1 is NOT connected to 4 = OK) ...but it should NOT be displayed twice, only because 2 and 3 are connected to 4!!

 

3->5 (1 is NOT connected to 5 = OK)

 

the only thing i could come up with, would look similar to the php code below. but id want to do all this within just one, simple MySQL query, if its possible (i.e. JOIN?).

 

$a = mysql_query("SELECT id2 FROM table WHERE id1 = 1");
while($b = mysql_fetch_assoc($a))
{
$c = mysql_query("SELECT id2 FROM table WHERE id1 = $b[id2]");
while($d = mysql_fetch_assoc($a))
{
	$e = mysql_query("SELECT id2 FROM table WHERE id1 = 1 AND id2 = $d[id2]");
	$f = mysql_fetch_assoc($e);

	if(!$f['id2'])
	{
		echo $f['id2'];
	}
}
}

 

id appreciate any help with this. thank you!

Link to comment
https://forums.phpfreaks.com/topic/209996-php-mysql-join/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.