简体   繁体   English

SQLite查询中的奇怪结果

[英]Strange result in SQLite query

I´m getting a strange result from a SQLite query. 我从SQLite查询中得到一个奇怪的结果。 The query is the next one: 该查询是下一个查询:

SELECT rule FROM rules
WHERE idRule = (SELECT idRuleForeign FROM rulesXfilter
                 WHERE idFilterForeign = (SELECT idFilter FROM filters
                                          WHERE name = 'Filter1'));

Now, let´s suppose that I have the following tables with a few rows on it. 现在,假设我有下面的表格,上面有几行。

      filters                rules              rulesXfilter
idFilter   name       idRule     rule      idRuleForeign  idFilterForeign
   1      Filter1       1         Rule1          1              1
   2      Filter2       2         Rule2          2              1
                        3         Rule3          3              1
                                                 2              2

What I get is {Rule1}, although I think I should get {Rule1, Rule2, Rule3} 我得到的是{Rule1},尽管我认为我应该得到{Rule1,Rule2,Rule3}

What am I doing wrong? 我究竟做错了什么?

Select idRuleForeign... returns multiple results, yes ( {1, 2, 3} ). Select idRuleForeign...返回多个结果,是( {1, 2, 3} )。 However, you then say "give me the rule where idRule = {SET}", and sql doesnt like this. 但是,然后您说“给我idRule = {SET}的规则”,而sql不喜欢这样。 I believe what is happening is that it is instead taking the first result only and giving you that. 相信正在发生的事情是,它只取得了第一个结果,而是给了您结果。

The solution is to use joins. 解决方案是使用联接。 Inner selects like that, while work most of the time, can REALLY slow down your query. 这样的内部选择在大多数时间都可以工作,但确实会降低查询速度。 If I got my syntax correct, the following should do what you need: 如果我的语法正确,则应该执行以下操作:

SELECT r.rule FROM rules r
    JOIN rulesXfilter rf ON r.idRule = rf.idRuleForeign
    JOIN filters f ON f.idFilter = rf.idFilterForeign
WHERE f.name = 'Filter1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM