Jump to content

array for excluding values


Tjk

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
Link to comment
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]

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.