4

这是我现有的查询,它成功地从两个表中选择不同的记录并将它们组合成一列:

SELECT index_text AS unique_text FROM words
UNION
SELECT c1index_text FROM words_content 
ORDER BY unique_text

现在我想消除所有记录WHERE body NOT IN (SELECT body FROM sms)(或者NOT EXISTS,任何工作都可以)。问题是,无论我尝试什么,每当我尝试使用括号时都会出现语法错误,或者它无法识别sms.body(即使我在每一列之前都放置了它的父表)。我在想一些 SQLite 限制可能使这比它需要的更难,但必须有一个解决方法。以下是我尝试失败的查询(我也尝试了这些查询的许多变体但无济于事):

SELECT index_text AS unique_text FROM words
UNION
SELECT c1index_text FROM words_content
  WHERE body NOT IN (SELECT body FROM sms)
ORDER BY unique_text

结果错误:No such column: body

SELECT words.index_text AS unique_text FROM words
UNION
SELECT words_content.c1index_text FROM words_content
LEFT JOIN sms
  ON sms.body=unique_text
ORDER BY unique_text

结果错误:No such column: unique_text

如何加入别名列并仅显示不存在的记录sms.body?谢谢,

4

1 回答 1

2

如果在 UNION 中使用 where 或 join 子句,则必须将它们应用于两个select语句。

SELECT index_text AS unique_text 
FROM words
where index_text NOT IN (SELECT body FROM sms)
UNION
SELECT c1index_text 
FROM words_content
WHERE c1index_text NOT IN (SELECT body FROM sms)
ORDER BY unique_text
于 2012-11-26T20:15:06.083 回答