Select one row in MySQL with both AND, OR statements

Discuss coding issues, and scripts related to PHP and MySQL.
Marius
Posts: 107

Select one row in MySQL with both AND, OR statements

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.

Admin Posts: 805
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')");

Similar Topics