Search code examples
sqlrandomposts

How to get random posts with specific IDs in SQL?


I need to fetch random posts which will be published in the index. I use this SQL query.

SELECT * FROM mt_item WHERE item_id= 1,2,3,4 ORDER by rand() LIMIT 4

Cannot we use "rand()" with specific IDs? What am I doing wrong? How can I achieve this? Thank you very much for your time.


Solution

  • Presumably, you want in . . .

    SELECT *
    FROM mt_item 
    WHERE item_id IN ( 1,2,3,4 )
    ORDER by rand() LIMIT 4