Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to include additional conditions inside a main WHERE clause? (like an if inside a higher-level if conditional)?

I have a table aus_woocommerce_order_itemmeta with the following schema:

aus_woocommerce_order_itemmeta

How can I create a query that yields the following logic?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT * FROM aus_woocommerce_order_itemmeta WHERE order_item_id  = 660
    /* And only from those items, the ones with order_item_id = 660
    SELECT the rows that meet any of the following conditions: */
meta_key LIKE "%bebida caliente%"
meta_key LIKE "%elige%"
meta_key  LIKE "%agregar%";

This is my current query:

SELECT order_item_id, meta_key, meta_value 
FROM aus_woocommerce_order_itemmeta WHERE order_item_id  = 660
AND meta_key LIKE "%bebida caliente%"
OR meta_key LIKE "%elige%"
OR meta_key  LIKE "%agregar%";

But obviously, the OR statements are on the same level as the first WHERE condition, so it’s returning everything, regardless of its order_item_id.

>Solution :

Just wrap the or statement in a parenthesis like this.

SELECT order_item_id, meta_key, meta_value 
FROM aus_woocommerce_order_itemmeta WHERE order_item_id  = 660
AND (meta_key LIKE "%bebida caliente%"
OR meta_key LIKE "%elige%"
OR meta_key  LIKE "%agregar%");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading