0

这个查询:

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应该有效:从 sqlite table 中选择随机行。有人可以解释为什么我的查询不起作用吗?

4

1 回答 1

2

复合查询中,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;
于 2015-07-19T11:26:21.860 回答