Mysql select with exceptions
I want to create a table with data from mysql, but with some exceptions. More exactly, I have:
$data = mysql_query("SELECT `id` FROM `dates` WHERE `user`='x'");
$exceptions = mysql_query("SELECT `id` FROM `exceptions` WHERE `user`='x'");
I want to make something like:
for($j=1; $date = mysql_fetch_object($data); $j++)
But I don't want to get rows from data, with the same ids from $exceptions, something like:
if($data->id == $exceptions->id) return null;
开发者_JAVA百科So if in $data I have 1, 2, 3, 4, 5 and on $exceptions I have 4, i want to return just rows 1, 2, 3, 5
You only need one query:
SELECT id FROM dates WHERE user = 'x' AND id NOT IN (SELECT id FROM exceptions WHERE user = 'x'))
There are multiple ways to write that query, too. You should always aim to retrieve only the data you need.
精彩评论