Jump to content


Photo

array for excluding values


  • Please log in to reply
2 replies to this topic

#1 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 20 May 2006 - 12:43 PM

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.

$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']; 
}


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

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 20 May 2006 - 02:43 PM

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] *
FROM `information` LEFT JOIN `friends` ON `information`.userid = `friends`.friend1
WHERE `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]



#3 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 21 May 2006 - 01:01 PM

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users