Forum Moderators: coopster

Message Too Old, No Replies

Trying to write query

         

Joe Belmaati

4:33 pm on May 13, 2008 (gmt 0)

10+ Year Member



Hi everyone,
I have read, studied, searched and everything mandatory prior to writing my question, which I am afraid is of rookie nature, and more related to MySQL than PHP. Unfortunately I could not find a MySQL forum, so here goes.

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 = ''";


I have dabbled with GROUP BY and HAVING but I don't seem to be able to get the right result set. I hope I have explained my problem well enough.

Thanks a bunch,
Joe

[edited by: Joe_Belmaati at 4:36 pm (utc) on May 13, 2008]

eelixduppy

4:37 pm on May 13, 2008 (gmt 0)



Try changing your query to just the following and see what you get. I don't see why you need the rest of the query to do what you want:

$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 :)

dublinmike

5:44 pm on May 13, 2008 (gmt 0)

10+ Year Member



Hi there,

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.