[英]Two queries combine select query
I need two queries combined into one.我需要将两个查询合并为一个。
This is the first query:这是第一个查询:
SELECT SUM(a.wert) as get_in, game
FROM gamelogs a
WHERE
a.state = 'bet'
AND a.game IN (31906,3300223,125140,7119,7130,1210,1141,7350,40002992,400030,40002994,7210,7353,3300099,7378,4000285,4000299,4000297,40002871,40002869,7229,40002867,7114,4000282,4000280,4000275,7093,4000273,4000270,7144,4000266,4000261,4000252,3300248,3300229,7087,102001,7123,3300095,7389,7384,7340,7356,7213,7079,7376,7360,7162,7161,7158,7157,7152,102010,7237,7305,7143,125112,7142,7139,7346,7137,7367,7132,7131,125126,6993,102014,7127,3300210,7124,7122,125134,7362,7354,3300142,7118,7117,125116,7234,7112,7110,7109,3300195,7106,7348,7364,7399,7374,7101,6998,7098,7225,7306,7368,7370,6996,7015,102000,3300134,7238,3100009,7381,1339,170133,3300239,1315,1358,1459,1522,1600,400031674,76000,7400,7242,400034,400032,400029846,400031677,4000295,400036,4000305,31910,319011,4000302,31915,319015,31919,31917,40002302,31921,7413,319017,319019,31955,31957,31923,31926,31908)
AND MONTH(a.date) = '3'
AND YEAR(a.date) = '2020'
GROUP BY a.game;
and the second query is:第二个查询是:
SELECT SUM(a.wert) as get_out, game
FROM gamelogs a
WHERE
a.state = 'win'
AND a.game IN (31906,3300223,125140,7119,7130,1210,1141,7350,40002992,400030,40002994,7210,7353,3300099,7378,4000285,4000299,4000297,40002871,40002869,7229,40002867,7114,4000282,4000280,4000275,7093,4000273,4000270,7144,4000266,4000261,4000252,3300248,3300229,7087,102001,7123,3300095,7389,7384,7340,7356,7213,7079,7376,7360,7162,7161,7158,7157,7152,102010,7237,7305,7143,125112,7142,7139,7346,7137,7367,7132,7131,125126,6993,102014,7127,3300210,7124,7122,125134,7362,7354,3300142,7118,7117,125116,7234,7112,7110,7109,3300195,7106,7348,7364,7399,7374,7101,6998,7098,7225,7306,7368,7370,6996,7015,102000,3300134,7238,3100009,7381,1339,170133,3300239,1315,1358,1459,1522,1600,400031674,76000,7400,7242,400034,400032,400029846,400031677,4000295,400036,4000305,31910,319011,4000302,31915,319015,31919,31917,40002302,31921,7413,319017,319019,31955,31957,31923,31926,31908)
AND MONTH(a.date) = '3'
AND YEAR(a.date) = '2020'
GROUP BY a.game;
Both queries are similar, only difference is state = bet/win.这两个查询是相似的,唯一的区别是 state = bet/win。 How can I combine them into one query where only game, get_in, get_out will be as columns?
如何将它们组合成一个查询,其中只有 game、get_in、get_out 将作为列? Do I need to make a join or an union, how will the query be?
我是否需要加入或联合,查询将如何?
With conditional aggregation:使用条件聚合:
SELECT
SUM(CASE WHEN a.state = 'bet' THEN a.wert END) as get_in,
SUM(CASE WHEN a.state = 'win' THEN a.wert END) as get_out,
a.game
FROM gamelogs a
WHERE a.game IN (...) AND MONTH(a.date) = '3' AND YEAR(a.date) = '2020'
GROUP BY a.game;
If you don't want null
s in the results use coalesce()
to get 0
s like:如果您不想在结果中使用
null
s,请使用coalesce()
来获得0
s,例如:
COALESCE(SUM(CASE WHEN a.state = 'bet' THEN a.wert END), 0)
or:或者:
SUM(CASE WHEN a.state = 'bet' THEN a.wert ELSE 0 END)
we could do conditional aggregation.我们可以做条件聚合。
also, I would avoid wrapping the date
column in YEAR() and MONTH() functions in a predicate... that forces MySQL to evaluate those expressions of date.此外,我会避免将 YEAR() 和 MONTH() 函数中的
date
列包装在谓词中……这会强制 MySQL 评估这些日期表达式。 Referencing bare column allows for MySQL to make effective use of a range scan operation when a suitable index is available.当合适的索引可用时,引用裸列允许 MySQL 有效地使用范围扫描操作。
SELECT SUM(IF(a.state= 'bet' ,a.wert,NULL)) AS get_in
, SUM(IF(a.state= 'win' ,a.wert,NULL)) AS get_out
, a.game
FROM gamelogs a
WHERE a.state IN ('bet','win')
AND a.game IN (31906,3300223,125140,7119,7130,1210,1141,7350,40002992,400030,40002994,7210,7353,3300099,7378,4000285,4000299,4000297,40002871,40002869,7229,40002867,7114,4000282,4000280,4000275,7093,4000273,4000270,7144,4000266,4000261,4000252,3300248,3300229,7087,102001,7123,3300095,7389,7384,7340,7356,7213,7079,7376,7360,7162,7161,7158,7157,7152,102010,7237,7305,7143,125112,7142,7139,7346,7137,7367,7132,7131,125126,6993,102014,7127,3300210,7124,7122,125134,7362,7354,3300142,7118,7117,125116,7234,7112,7110,7109,3300195,7106,7348,7364,7399,7374,7101,6998,7098,7225,7306,7368,7370,6996,7015,102000,3300134,7238,3100009,7381,1339,170133,3300239,1315,1358,1459,1522,1600,400031674,76000,7400,7242,400034,400032,400029846,400031677,4000295,400036,4000305,31910,319011,4000302,31915,319015,31919,31917,40002302,31921,7413,319017,319019,31955,31957,31923,31926,31908)
AND a.date >= '2020-03-01'
AND a.date < '2020-03-01' + INTERVAL 1 MONTH
GROUP
BY a.game
Note that this has the potential to return values for get_in and/or get_out, rows that were not returned by the two separate queries.请注意,这有可能返回 get_in 和/或 get_out 的值,即两个单独查询未返回的行。 (Consider example game = 31906 that has no row for "win"...
(考虑示例游戏 = 31906 没有“获胜”行......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.