SQLite:...除了...随机排序()
Posted
技术标签:
【中文标题】SQLite:...除了...随机排序()【英文标题】:SQLite: ...EXCEPT ... ORDER BY RANDOM() 【发布时间】:2015-07-19 11:13:16 【问题描述】:这个查询:
SELECT colour FROM ColourView EXCEPT SELECT colour FROM SkinColourExceptionsView WHERE race = 'Human' ORDER BY RANDOM() LIMIT 1
产生以下异常:1st ORDER BY term does not match any column in the result set
但是,将ORDER BY RANDOM()
更改为ORDER BY colour
能够产生结果(按颜色字母顺序而不是随机排序)。以下建议ORDER BY RANDOM() LIMIT 1
应该工作:Select random row from an sqlite table。有人可以解释为什么我的查询不起作用吗?
【问题讨论】:
【参考方案1】:在compound query 中,ORDER BY 术语必须与结果集中的某些列匹配。
您可以通过将复合查询移动到子查询中来解决此限制:
SELECT colour
FROM (SELECT colour FROM ColourView
EXCEPT
SELECT colour FROM SkinColourExceptionsView WHERE race = 'Human')
ORDER BY random()
LIMIT 1;
或者,重写查询以使用不同的过滤机制:
SELECT colour
FROM ColourView
WHERE colour NOT IN (SELECT colour
FROM SkinColourExceptionsView
WHERE race = 'Human')
ORDER BY random()
LIMIT 1;
【讨论】:
两个答案都很好,解释是最重要的!非常感谢先生。以上是关于SQLite:...除了...随机排序()的主要内容,如果未能解决你的问题,请参考以下文章