从哪里选择SQLite表中的随机行?

时间:2012-07-20 02:49:09

标签: sqlite

我有一张这样的表:

CREATE TABLE records (_id INTEGER PRIMARY KEY,
                    question TEXT, answer TEXT, category INTEGER)

给出一个特定的类别值(假设为2),我想从该类别中选择一个随机行。这就是我想象的解决方案:

Select   question, answer, category //I think there is something missing
From     records
Where    category=2

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

你可以这样做:

SELECT question, answer, category
FROM records
WHERE category=2 ORDER BY RANDOM() LIMIT 1;