我想要一个SELECT查询,它可以获得user1&的8个特定组合。 user2(用户2和用户1的组合也值得拥有而不是多余的)。 “IN”声明似乎值得。
直接相关的问题。 1.“WHERE IN”语句可以嵌套吗?
(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)
...等?[此时运行查询并查看相关信息似乎更容易。]
当前的想法:
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;'
答案 0 :(得分:3)
查询can have only one WHERE
clause,但该表达式可以将多个条件与AND
或OR
结合使用:
SELECT ...
FROM ...
WHERE programquality.pq IS NOT NULL AND
simscores.user1 IN ('abraham', 'billy', ...) AND
simscores.user2 IN ('abraham', 'billy', ...)
但是,这些IN
表达式不允许您匹配user1
和user2
的特定值。
你无法避免列出你想要的所有组合。
但是,您可以通过检查有效组合的组合名称来稍微简化表达式:
... WHERE ... AND
simscores.user1 || '+' || simscores.user2
IN ('billy+sue', 'sue+billy',
'jack+jill', 'jill+jack',
...)