Jump to content

Archived

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

Tjk

array for excluding values

Recommended Posts

I'm having some trouble coming up with a method of doing this...

Run a mysql query and select userids from one table. I then don't want these userids to be selected/displayed in the second query I run in a different table.

[code]
$query= mysql_query("SELECT * from friends WHERE friend1='$userid'") or die(mysql_error());
while($row= mysql_fetch_assoc($query)){
echo $row['userid'];
}

//some more code

// this query should not display the userids from displayed in the query above


$query= mysql_query("SELECT * from information WHERE userid='$userid'") or die(mysql_error());
while($row= mysql_fetch_assoc($query)){
echo $row['userid'];
}

[/code]

I was wondering if I could create an array of the userids selected from the 1st query and then tell the mysql to exclude them in the second query but I wasn't sure if this was possible.

Any ideas on how I can do this?

Regards,
-Tjk

Share this post


Link to post
Share on other sites
It can be done with one query. Try the SQL below and that should work if I understand you correctly. It will display all the rows in the "information" table that don't match one in the "friends" table.

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] *
[color=green]FROM[/color] [color=orange]`information`[/color] [color=green]LEFT[/color] [color=green]JOIN[/color] `friends` ON `information`.userid [color=orange]=[/color] `friends`.friend1
[color=green]WHERE[/color] `friends`.friend1 IS NULL [!--sql2--][/div][!--sql3--]

Example:

If the "information" table had these rows of userid's:

1
2
3
4
5

And the "friends" table had these rows of friend1's:

1
2
3

The SQL would select and display just:

4
5


FYI:

Join tutorial:
[a href=\"http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.shtml\" target=\"_blank\"]http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.shtml[/a]

Share this post


Link to post
Share on other sites
EDIT: Thanks for all your help. I had play around with it a bit but I understand it and have been able to get it to work.

Regards,
-Tjk

Share this post


Link to post
Share on other sites

×

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.