2
| 邮政编码 | 价值 | 标题 |
----------------------------
| 1 | 0 | 标题 1 |
| 2 | 1 | 标题 2 |
| 3 | 1 | 标题 3 |
| 4 | 0 | 标题 4 |
| 5 | 4 | 标题 5 |

我试图从我的表中选择两个随机不同的行。有没有办法用 SQL 做到这一点?

我试过了

SELECT postid
FROM table
WHERE postid > 0.9
ORDER BY RAND( )
LIMIT 2 
4

2 回答 2

3

试试这个

SELECT DISTINCT postid 
FROM tablename
ORDER BY RAND()
LIMIT 2
于 2012-10-17T18:51:47.083 回答
2

是的,与andDISTINCT一起使用:ORDER BY RAND()LIMIT

SELECT
  DISTINCT
  postid,
  value,
  title
FROM yourtable
ORDER BY RAND()
LIMIT 2
于 2012-10-17T18:49:22.860 回答