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

后端 未结 5 1204
南方客
南方客 2021-02-03 23:37

I need to select multiple records

I use

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

this

5条回答
  •  长发绾君心
    2021-02-04 00:08

    If you do

    SELECT *
    FROM `table`
    WHERE `ID` IN (5623, 5625, 5628, 5621)
    

    You should be fine, as long as the number of ID's don't get too big. I'd suggest you create an index for the ID's, so the query will perform faster.

    Also, consider changing your logic, so you don't have to pass that many ID's

提交回复
热议问题