2011-12-30 47 views
2

這裏的工作正常的查詢:SQLite的說:「沒有這樣的列:的rowid」使用子查詢時

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一個由設計限制?

+3

你可以嘗試在子查詢中別名rowid嗎? 像這樣:SELECT m2.rowid as match_rowid FROM messages m2 WHERE x LIKE'%abc%' – GuZzie 2011-12-30 18:13:24

+1

@GuZzie Whoa ...在*子查詢中別名*沒有幫助,但別名*只是*外部的,因爲'm1.rowid',修復它! – 2011-12-30 18:27:46

+0

太棒了! :D很高興聽到你解決了這個問題 – GuZzie 2012-01-01 19:33:30

回答

7

由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 
+2

「出於某種原因」 - 請參閱我上面的評論,http://stackoverflow.com/questions/8682398/sqlite-says-no-such-column-rowid-when-using-sub-queries#comment68721434_8682398 – LarsH 2016-11-22 17:33:59