开发者

Filter select in SQLite using single query

开发者 https://www.devze.com 2023-03-29 15:38 出处:网络
I need to query a table like this: select phrase_id from direct_words where knowledge_id = X and dictionary_word_id = Y;

I need to query a table like this:

select phrase_id
from direct_words 
where knowledge_id = X and dictionary_word_id = Y;

but I only want to retrieve the rows where the same pair (knowledge_id = X and dictionary_word_id = Y) doesn't exist in another table (top_words). This can be done, applying the query:

select top_id from top_words where knowledge_id = X and dictionary_word_id = Y;

for each returned result from the first query and rejecting if the开发者_如何转开发 second query has a row count above 0.

Is this possible to do in a single query to SQLite?


Either the left join as Andrey suggested, or ...AND NOT EXISTS ( select x from TOP_WORDS where .... )


select dw.phrase_id
from direct_words dw
left outer join top_words tw on dw.phrase_id=tw.phrase_id
where dw.knowledge_id = X and dw.dictionary_word_id = Y and tw.phrase_id is NULL

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号