Help with the logic of a query for MySQLi


Greetings guys I have the following sentence:

$query = "SELECT * 
FROM pedidos  
WHERE usuario = '$usua
AND status_pedido = 'ESPERANDO' ";

Just as it works perfectly but I would like it to also select if the condition is met status_qualified = 'APPROVED'

I tried placing the conditional

OR status_pedido = 'APROBADO'

But it does not give me results even if there is a data with status_pedido = 'WAITING' or 'APPROVED'

asked by Jose M Herrera V 10.10.2018 в 16:10

3 answers


Try using the expression IN like this:

$query = "SELECT * FROM pedidos  
WHERE usuario = '$usua'
AND status_pedido IN('ESPERANDO','APROBADO')";

expr IN (value, ...)


Returns 1 if expr equals any of the values in the list   IN; otherwise, it returns 0. If all values are constant,   they are evaluated according to the type of expr and ordered. The search   of the element is done by a binary search. This means   that IN is very fast if the list of IN values is formed   completely by constants.

answered by 10.10.2018 / 16:14

You can use the IN command like this:

AND status_pedido IN ('ESPERANDO','TERMINADO')

Reference: link

Or use OR, for this you should also group the conditions like this:

AND (status_pedido = 'ESPERANDO' or status_pedido = 'TERMINADO')

Reference: link

answered by 10.10.2018 в 16:31

Maybe you can try this way

$query = "SELECT * 
FROM pedidos  
WHERE usuario = '$usua
(status_pedido = 'ESPERANDO') OR
(status_pedido = 'APROBADO')";

We use the operator AND to request that a second one be used   condition, which in turn is divided into 2 parts enclosed between   parentheses, where we first indicate that the status is equal to   WAITING and using the operator OR after we clarify that if it is not   waiting can be approved and it should work for you too

answered by 10.10.2018 в 16:42