Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to select multiple rows by ID in sql?

I need to select multiple records

I use

SELECT * FROM `table` WHERE `ID` =5623    OR `ID` =5625    OR `ID` =5628    OR `ID` =5621 

this query runs 4 times per second with php

is there a better and faster way for this ?

like image 913
lashX Avatar asked Mar 04 '10 12:03

lashX


People also ask

How do I select multiple row values in SQL?

Multiple row subquery returns one or more rows to the outer SQL statement. You may use the IN, ANY, or ALL operator in outer query to handle a subquery that returns multiple rows.

How do I select multiple data in SQL?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.


1 Answers

SELECT * FROM `table` where ID in (5263, 5625, 5628, 5621)  

is probably better, but not faster.

like image 96
anthares Avatar answered Oct 15 '22 19:10

anthares