我有两个具有以下结构的表:
WEEKLY MONTHLY
|Name|Kills|Deaths| |Name|Kills|Deaths|
__________________ __________________
|B | 1 | 12 | |B | 43 | 12 |
|A | 3 | 25 | |A | 9 | 25 |
|D | 4 | 2 | |D | 8 | 2 |
|C | 5 | 4 | |C | 5 | 4 |
|F | 2 | 8 | |F | 10 | 8 |
我需要通过杀/死率来检索它们,我相信这可以用MySQL做,但是当我意识到需要将两个表合并然后命令结果时,事情变得棘手。
以下适用于1张桌子:
mysql> SELECT * FROM weekly ORDER BY kills/deaths DESC;
但我怎样才能在两张桌子上做到这一点?
答案 0 :(得分:2)
尝试:
SELECT WEEKLY.Name, WEEKLY.Kills+MONTHLY.Kills Kills, WEEKLY.Deaths+MONTHLY.Deaths Deaths
FROM WEEKLY INNER JOIN MONTHLY ON WEEKLY.Name=MONTHLY.Name
ORDER BY (WEEKLY.Kills+MONTHLY.Kills)/(WEEKLY.Deaths+MONTHLY.Deaths) DESC;
答案 1 :(得分:1)
如果我们需要通过结合这两个表来结合:
SELECT * FROM (SELECT * FROM weekly UNION SELECT * FROM monthly) u
ORDER BY kills / deaths DESC;
如果我们需要通过添加相同名称的杀戮和死亡来结合使用:
SELECT
u.name,
SUM(u.kills) kills,
SUM(u.deaths) deaths,
IFNULL(SUM(u.kills) / SUM(u.deaths), 2147483647) ratio
FROM (SELECT * FROM weekly UNION SELECT * FROM monthly) u
GROUP BY u.name
ORDER BY ratio DESC, kills DESC;
答案 2 :(得分:1)
SELECT a.*,b.*
FROM weekly a inner join MONTHLY b
on a.name = b.name
ORDER BY (a.kills+b.kills)/(a.deaths+b.deaths) DESC;