Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Query a List of Values

Tags:

mysql

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' 
like image 378
JMC Avatar asked Jan 20 '12 17:01

JMC


People also ask

How do I SELECT a list in MySQL?

mysql> select *from ListOfValues where Age IN(20,21,23,25,26,27,28) -> order by field(Age,20,21,23,25,26,27,28);

What is query for list in MySQL?

A list of commonly used MySQL queries to create database, use database, create table, insert record, update record, delete record, select record, truncate table and drop table are given below.

Can you have a list in MySQL?

The most common way to get a list of the MySQL databases is by using the mysql client to connect to the MySQL server and run the SHOW DATABASES command. If you haven't set a password for your MySQL user you can omit the -p switch.

How do I display the contents of a table in MySQL?

The first command you will need to use is the SELECT FROM MySQL statement that has the following syntax: SELECT * FROM table_name; This is a basic MySQL query which will tell the script to select all the records from the table_name table.


2 Answers

You can use the IN operator

`cat` IN ('12', '18', '30') 
like image 62
Shraddha Avatar answered Sep 21 '22 21:09

Shraddha


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') 
like image 44
Sergio Tulentsev Avatar answered Sep 20 '22 21:09

Sergio Tulentsev