Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write "WHERE foo = 'bar' OR foo = 'baz' OR ..." without repeating "foo" each time

Tags:

sql

mysql

where

I'm new to MySQL and was wondering; instead of doing

SELECT fields FROM table WHERE name="tim" OR name="bob" OR name="nancy" OR name="john"

If I could do something like

SELECT fields FROM table WHERE (name="tim" OR "bob" OR "nancy" OR "john")
like image 948
James T Avatar asked Dec 13 '22 20:12

James T


2 Answers

Use the IN clause:

SELECT fields FROM table WHERE name IN ("tim", "bob", "nancy", "john");
like image 170
BoltClock Avatar answered Apr 27 '23 06:04

BoltClock


You can do like -

SELECT fields FROM table WHERE name in("tim","bob","nancy","john")
like image 29
Sadat Avatar answered Apr 27 '23 06:04

Sadat