Forum Moderators: coopster
I am trying to return a result set using one query. What I am trying to do is to SELECT based on a set of conditions. One condition is that I want results for a row if either of three columns are empty.
This is what I've tried:
$sql = "SELECT * FROM " . MY_TABLE . "
WHERE user_id = $user_id
AND column1 = ''
OR column2 = ''
OR column3 = ''"; Thanks a bunch,
Joe
[edited by: Joe_Belmaati at 4:36 pm (utc) on May 13, 2008]
$sql = "SELECT * FROM " . MY_TABLE . " WHERE user_id = $user_id";
Just make sure to escape your variable.
Also, we have a databases forum: [webmasterworld.com...] in case you need it :)
Instead of:
$sql = "SELECT * FROM " . MY_TABLE . "
WHERE user_id = $user_id
AND column1 = ''
OR column2 = ''
OR column3 = ''";
try...
$sql = "SELECT * FROM " . MY_TABLE . "
WHERE user_id = $user_id
AND (column1 = ''
OR column2 = ''
OR column3 = '')";
The only difference is the brackets in the where clause. Best to eliminate the ambiguity of operator precedence so this means get all rows from your table where the user_id is matched and where one of the columns is empty. Your query would pull back rows where column3 is empty but which don't necessarily apply to that particular user for example. Hope this helps.