Home » Mysql » MySQL Query a List of Values

MySQL Query a List of Values

Posted by: admin November 30, 2017 Leave a comment

Questions:

I want to create a query from a list of values and return data for every match of cat.

This works but it’s not requiring the options value. What’s the easier way to query a list of values?

SELECT * 
FROM `table1` 
WHERE `option`='R' && `cat`='12' || `cat`='18' || `cat`='30'
Answers:

You can use the IN operator

`cat` IN ('12', '18', 30')

Questions:
Answers:

You probably forgot to enclose those OR parts into parentheses

SELECT * 
FROM `table1` 
WHERE `option`='R' and (`cat`='12' or `cat`='18' or `cat`='30')

Questions:
Answers:

SELECT *
FROM table1
WHERE option=’R’ and cat IN (’12’, ’18’, 30′)