Page 1 of 1

Select one row in MySQL with both AND, OR statements

Posted: 27 Nov 2014, 07:42
by Marius
Hello,
I have this Select query, I want to select one row from mysql table, where id = $id, and has "x_name" or "y_name" 'yes'.

Code: Select all

$insert_cat = $mysqli->query("SELECT * FROM `prod_site` WHERE id = $id AND x_name = 'yes' OR y_name = 'yes'");
But it returns more rows.
I don't know what is wrong in this query.

Select one row in MySQL with both AND, OR statements

Posted: 27 Nov 2014, 07:50
by Admin
Hi
The problem is that you didn't separate the conditions with the OR from the id=$id, so to select just the row which has that $id AND 'yes' in any of the other columns.
Try this Select query:

Code: Select all

$insert_cat = $mysqli->query("SELECT * FROM `prod_site` WHERE id = $id AND (x_name = 'yes' OR y_name = 'yes')");