我不認爲這個查詢會做你想做的。你會生成查詢會是這樣的組合=(1,2):
SELECT words.* FROM words INNER JOIN syllables ON words.id = syllables.word_id WHERE syllables.syllable = 1 AND syllables.syllable = 2
我想你想的是這對組合=(1,2):
SELECT words.* FROM words INNER JOIN syllables AS s1 ON words.id = s1.word_id AND s1.syllable = 1 INNER JOIN syllables AS s2 ON words.id = s2.word_id AND s2.syllable = 2
重複加入可能會很慢,但這裏是你如何能在SQLAlchemy中做到這一點:
query = db.session.query(Word)
for syl in combo:
syl_cls = aliased(Syllable)
query = query.join(syl_cls, Word.syllables).filter(syl_cls.syllable == syl)
matches = query.all()
如果你生成或條款則此查詢將是一個簡單得多。