PHP MySQL select random rows

前端 未结 6 1490
深忆病人
深忆病人 2021-02-15 16:47

I have a problem selecting 6 random friends

This is the query I\'ve got so far:

$result = num_rows(\"SELECT * FROM friends WHERE member_id = \'\".$_S         


        
6条回答
  •  星月不相逢
    2021-02-15 17:45

    Instead of SELECT * at the beginning, try SELECT COUNT(*) and use the actual return value instead of num_rows().

    Your loop could generate duplicates. I would suggest trying OMG Ponies answer.

    There is a whole chapter about random selection in the book SQL Antipatterns.

提交回复
热议问题