Have more than one line in the same query

Asked

Viewed 44 times

1

I cannot return the database data when I try to search for more products related to different categories. I am using the following query;

SELECT * FROM `app_product` WHERE app_category = 6 AND app_sub_category = 181

In that query i get all the data that is in the category 6 and subcategory 181. So far so good, I need a way to search a single query more data for example I want all data with the category 6 and subcategory 181 and 182 without having to create a query for each condition this way:

SELECT * FROM `app_product` WHERE app_category = 6 AND app_sub_category = 181
SELECT * FROM `app_product` WHERE app_category = 6 AND app_sub_category = 182

Is there any Mysql function I can use in this case?

2 answers

5


The same way you used a AND, just use a OR when you want one or the other. But more than learning how it does my suggestion is to study relational logic.

SELECT * FROM `app_product` WHERE app_category = 6 AND (app_sub_category = 181 OR app_sub_category = 182)

I put in the Github for future reference.

Could use the operator of IN as in the other answer, but for two it is usually more common something like this, depending on the database and version can be more performatic to do one or the other.

4

You can also use the operator IN which is an abbreviation of several conditions OR where you can specify several values in a clause WHERE.

SELECT * FROM `app_product` WHERE app_category = 6 AND app_sub_category IN (181, 182);

Just like you can do to return all subcategories except x, y, z...

SELECT * FROM `app_product` WHERE app_category = 6 AND app_sub_category NOT IN (1, 2, 3);
  • In this condition I found it better and more abbreviated than (app_sub_category = 181 OR app_sub_category = 182)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.