| postid | value | title | ---------------------------- | 1 | 0 | Title 1 | | 2 | 1 | Title 2 | | 3 | 1 | Title 3 | | 4 | 0 | Title 4 | | 5 | 4 | Title 5 |
我试图从我的表中选择两个随机不同的行。有没有办法用SQL做到这一点?
我试过
SELECT postid
FROM table
WHERE postid > 0.9
ORDER BY RAND( )
LIMIT 2
答案 0 :(得分:3)
试试这个
SELECT DISTINCT postid
FROM tablename
ORDER BY RAND()
LIMIT 2
答案 1 :(得分:2)
是的,使用DISTINCT
以及ORDER BY RAND()
和LIMIT
:
SELECT
DISTINCT
postid,
value,
title
FROM yourtable
ORDER BY RAND()
LIMIT 2