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