Imran Hussain
Imran Hussain

Reputation: 25

Fetching multiple rows with specific IDs?

I want to fetch some specific rows from MySQL database using multiple ids, for example if I have to select only rows 201,567,991 etc...

like this

$sql_query = "SELECT * from tbl_name WHERE id = '201', '567', 'id-etc.'";

is it possible in MySQL?

Upvotes: 0

Views: 356

Answers (3)

Charles Bretana
Charles Bretana

Reputation: 146469

Use tge "IN" operator as in:|

 SELECT * from tbl_name WHERE id In ('201', '567', 'id-etc.')

Upvotes: 1

Aaron W.
Aaron W.

Reputation: 9299

Yes. Use IN Link

$sql_query = "SELECT * from tbl_name WHERE id IN ('201', '567')";

Upvotes: 1

Bojangles
Bojangles

Reputation: 101473

IN() should do the trick. Just put your list of numbers inside the brackets like so:

$sql_query = "SELECT * from tbl_name WHERE id IN(1, 2, 3, ...)";

You don't need to quote numbers going into INT fields, however it's a very good idea to cast any variables to int, to minimise security vulnerability:

$var = (int)$var;

If you're using strings, however, do quote them as you normally would.

Upvotes: 2

Related Questions