2

我想要一个 SELECT 查询,它可以获取 user1 和 user2 的 8 个特定组合(并且 user2 和 user1 的组合也值得拥有,而不是多余的)。“IN”声明似乎是值得的。

直接相关的问题。1.“WHERE IN”语句可以嵌套吗?

  1. 如果没有,我如何最有效/最容易地构建一个查询以便我可以指定(对于 4 对,其中 user1 和 user2 切换位置 - 8 个组合): (where user1 = billy AND user2 = sue) OR (where user1 = sue AND user2 =billy) OR (user1 = jack AND user2 = jill) OR (user1 = jill AND user2 = jack)...等?

[此时运行查询和 grep 相关信息似乎更容易。]

目前的想法:

sqlite3 -header -separator , some.db "SELECT DISTINCT programquality.time, simscores.user1, simscores.user2, simscores.simscore, programquality.gf, programquality.ga, programquality.pq FROM simscores LEFT JOIN programquality ON programquality.time = simscores.time AND programquality.username = simscores.user1 WHERE programquality.pq IS NOT NULL WHERE simscores.user1 IN ("abraham","billy","carl","dave","ethan","frank","george","harry") WHERE simscores.user2 IN ("abraham","billy","carl","dave","ethan","frank","george","harry");"

我用过这个,但显示了一些不相关的数据。

sqlite3 -header -separator , some.db 'SELECT DISTINCT programquality.time, simscores.user1, simscores.user2, simscores.simscore, programquality.gf, programquality.ga, programquality.pq FROM simscores LEFT JOIN programquality ON programquality.time = simscores.time AND programquality.username = simscores.user1 WHERE (simscores.user1  = "billy" OR simscores.user1 = "suzy" OR simscores.user1 = "john") AND (simscores.user2  = "billy" OR simscores.user2 = "suzy" OR simscores.user2 = "john") AND programquality.pq IS NOT NULL AND programquality.time IS NOT NULL;'
4

1 回答 1

4

一个查询只能有一个WHERE子句,但该表达式可以将多个条件与ANDor结合起来OR

SELECT ...
FROM ...
WHERE programquality.pq IS NOT NULL AND
      simscores.user1 IN ('abraham', 'billy', ...) AND
      simscores.user2 IN ('abraham', 'billy', ...)

但是,这些IN表达式不允许您匹配和的特定值。您无法避免列出您想要的所有组合。user1user2

但是,您可以通过检查组合名称的有效组合来稍微简化表达式:

... WHERE ... AND
          simscores.user1 || '+' || simscores.user2
           IN ('billy+sue', 'sue+billy',
               'jack+jill', 'jill+jack',
               ...)
于 2012-10-25T17:20:35.607 回答