如果我有以下数据,获得gameId 1411051547(下面列出)结果的最有效查询是什么?此外,这将被称为什么类型的查询?我正在学习MySQL,在尝试了解这些类型的查询时,我不确定要搜索什么。
输入数据:
championId IN (19, 23, 40) AND win=True
AND
championId IN (7, 18) AND win=False
以下是数据库中数据的设置方式:
mysql> SELECT gameId,championId,win from match_up WHERE gameId=1411051547;
+------------+------------+-----+
| gameId | championId | win |
+------------+------------+-----+
| 1411051547 | 7 | 0 |
| 1411051547 | 18 | 0 |
| 1411051547 | 19 | 1 |
| 1411051547 | 23 | 1 |
| 1411051547 | 31 | 0 |
| 1411051547 | 40 | 1 |
| 1411051547 | 45 | 1 |
| 1411051547 | 77 | 0 |
| 1411051547 | 236 | 1 |
| 1411051547 | 412 | 0 |
+------------+------------+-----+
10 rows in set (0.00 sec)
答案 0 :(得分:1)
您似乎正在寻找OR条款
SELECT gameId,championId,win from match_up
WHERE gameId=1411051547 AND
(
(championId IN (19, 23, 40) AND win=True)
OR
(championId IN (7, 18) AND win=False)
)
取决于win
字段的定义,使用1代替True可能更安全,而0代替false可能更安全。
答案 1 :(得分:1)
SELECT gameId
from match_up
group by gameId
having sum(championId IN (19, 23, 40) AND win = 1) = 3
and sum(championId IN (7, 18) AND win = 0) = 2
您需要按gameId
进行分组,以分析每个组的数据,因为您要检查多个记录,而不只是一个。
having
是组的where
子句。您可以使用像sum()
这样的聚合函数来检查整个组的条件,而不仅仅是单个记录。
答案 2 :(得分:0)
在查询中没有gameId,并使用amenadiel建议的OR:
select distinct gameId from match_up WHERE (
(championId IN (19, 23, 40) AND win=True)
OR
(championId IN (7, 18) AND win=False)
);