Here's a query that works fine:
SELECT rowid as msg_rowid, a, b, c FROM messages m1
And here's another one that's also fine:
SELECT rowid as match_rowid FROM messages m2 WHERE x LIKE '%abc%'
But if I put them together as follows, SQLite complains:
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
giving what looks like a misleading error message: No such column: rowid
.
How can I fix this?
The query runs fine if I use a messages.timestamp field instead of 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
Is this a bug, or a by-design restriction on the use of rowid
?
Answered by GuZzie in a comment. For some reason SQLite requires me to be explicit about the outer 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With