SQLite在使用子查询时说“没有这样的列:rowid”

时间:2011-12-30 17:57:25

标签: sqlite

这是一个可以正常运行的查询:

SELECT rowid as msg_rowid, a, b, c FROM messages m1

这是另一个也没关系的那个:

SELECT rowid as match_rowid FROM messages m2 WHERE x LIKE '%abc%'

但如果按照以下方式将它们放在一起,SQLite会抱怨:

SELECT rowid as msg_rowid, a, b, c FROM messages m1
JOIN
    (SELECT rowid as match_rowid FROM messages m2 WHERE x LIKE '%abc%')
ON
    msg_rowid >= match_rowid - 10 AND msg_rowid <= match_rowid + 5

给出看似误导性的错误消息:No such column: rowid

我该如何解决这个问题?

如果我使用messages.timestamp字段而不是rowid:

,则查询运行正常
SELECT timestamp as msg_ts, a, b, c FROM messages m1
JOIN
    (SELECT timestamp as match_ts FROM messages m2 WHERE x LIKE '%abc%')
ON
    msg_ts >= match_ts - 10 AND msg_ts <= match_ts + 5

这是一个错误,还是对使用rowid

的设计限制

1 个答案:

答案 0 :(得分:8)

GuZzie在评论中回答。出于某种原因,SQLite要求我明确外部rowid:

SELECT m1.rowid as msg_rowid, a, b, c FROM messages m1
       ^^ HERE
JOIN
    (SELECT rowid as match_rowid FROM messages m2 WHERE x LIKE '%abc%')
ON
    msg_rowid >= match_rowid - 10 AND msg_rowid <= match_rowid + 5