php/mysql - 比较 2 个表中的多个列并显示最大差异
php/mysql - Compare multiple columns from 2 tables and show biggest differnce
我有 1 个包含我所有信息的主要 table 和 1 个具有几乎相同结构但值略有不同的 table。现在我想比较这些值并得到最高的差异。我通过对每一列 (a1-a5) 的查询来处理这个问题,在每个查询的循环中我再次查询并对结果进行一些计算......我不知道,它很乱,我不知道如何得到这个以一种干净的方式。
这是2个table的结构:
+----------+----------+--------+-----+-----+-----+-----+-----+--------------+
| HomeTeam | AwayTeam | Result | a1 | a2 | a3 | a4 | a5 | Date |
+----------+----------+--------+-----+-----+-----+-----+-----+--------------+
| team1 | team2 | H | 5 | 2 | 4 | 4 | 3 | 11-04-2017 |
| team3 | team4 | A | 6 | 8 | 4 | 2 | 6 | 11-04-2017 |
| team1 | team3 | D | 3 | 7 | 9 | 7 | 2 | 12-04-2017 |
| team4 | team2 | H | 2 | 4 | 8 | 6 | 5 | 12-04-2017 |
+----------+----------+--------+-----+-----+-----+-----+-----+--------------+
+----------+----------+-----+-----+-----+-----+-----+
| HomeTeam | AwayTeam | a1 | a2 | a3 | a4 | a5 |
+----------+----------+-----+-----+-----+-----+-----+
| team1 | team2 | 2 | 3 | 5 | 3 | 3 |
| team3 | team4 | 5 | 7 | 5 | 2 | 3 |
| team1 | team3 | 4 | 2 | 8 | 6 | 5 |
| team4 | team2 | 2 | 5 | 5 | 6 | 4 |
+----------+----------+-----+-----+-----+-----+-----+
这就是我要打印的内容:
team 1 vs team 2 biggest increase was 150% (a1 -> 5 vs 2) on 11-04-2017
team 3 vs team 4 biggest increase was 100% (a5 -> 6 vs 3) on 11-04-2017
team 1 vs team 3 biggest increase was 250% (a2 -> 7 vs 2) on 12-04-2017
team 4 vs team 2 biggest increase was 60% (a3 -> 8 vs 5) on 12-04-2017
当我输入这个时,我在想也许只使用一个查询从两个 table 中获取所有内容并使用这些值进行一些计算?不确定..有人可以在正确的方向上给我很大的帮助吗?
我们可以在这里使用GREATEST
来找到每场比赛的最大点差:
SELECT
HomeTeam,
AwayTeam,
GREATEST(t1.a1 - t2.a1, t1.a2 - t2.a2, t1.a3 - t2.a3, t1.a4 - t2.a4, t1.a5 - t2.a5) AS max_diff
FROM table1 t1
INNER JOIN table2 t2
ON t1.HomeTeam = t2.HomeTeam AND t1.AwayTeam = t2.AwayTeam;
如果您还需要实际的 a
值,那么工作量会增加很多,尤其是考虑到您的数据未标准化。对于比上述更复杂的任何事情,您可能想要规范化数据。
我有 1 个包含我所有信息的主要 table 和 1 个具有几乎相同结构但值略有不同的 table。现在我想比较这些值并得到最高的差异。我通过对每一列 (a1-a5) 的查询来处理这个问题,在每个查询的循环中我再次查询并对结果进行一些计算......我不知道,它很乱,我不知道如何得到这个以一种干净的方式。
这是2个table的结构:
+----------+----------+--------+-----+-----+-----+-----+-----+--------------+
| HomeTeam | AwayTeam | Result | a1 | a2 | a3 | a4 | a5 | Date |
+----------+----------+--------+-----+-----+-----+-----+-----+--------------+
| team1 | team2 | H | 5 | 2 | 4 | 4 | 3 | 11-04-2017 |
| team3 | team4 | A | 6 | 8 | 4 | 2 | 6 | 11-04-2017 |
| team1 | team3 | D | 3 | 7 | 9 | 7 | 2 | 12-04-2017 |
| team4 | team2 | H | 2 | 4 | 8 | 6 | 5 | 12-04-2017 |
+----------+----------+--------+-----+-----+-----+-----+-----+--------------+
+----------+----------+-----+-----+-----+-----+-----+
| HomeTeam | AwayTeam | a1 | a2 | a3 | a4 | a5 |
+----------+----------+-----+-----+-----+-----+-----+
| team1 | team2 | 2 | 3 | 5 | 3 | 3 |
| team3 | team4 | 5 | 7 | 5 | 2 | 3 |
| team1 | team3 | 4 | 2 | 8 | 6 | 5 |
| team4 | team2 | 2 | 5 | 5 | 6 | 4 |
+----------+----------+-----+-----+-----+-----+-----+
这就是我要打印的内容:
team 1 vs team 2 biggest increase was 150% (a1 -> 5 vs 2) on 11-04-2017
team 3 vs team 4 biggest increase was 100% (a5 -> 6 vs 3) on 11-04-2017
team 1 vs team 3 biggest increase was 250% (a2 -> 7 vs 2) on 12-04-2017
team 4 vs team 2 biggest increase was 60% (a3 -> 8 vs 5) on 12-04-2017
当我输入这个时,我在想也许只使用一个查询从两个 table 中获取所有内容并使用这些值进行一些计算?不确定..有人可以在正确的方向上给我很大的帮助吗?
我们可以在这里使用GREATEST
来找到每场比赛的最大点差:
SELECT
HomeTeam,
AwayTeam,
GREATEST(t1.a1 - t2.a1, t1.a2 - t2.a2, t1.a3 - t2.a3, t1.a4 - t2.a4, t1.a5 - t2.a5) AS max_diff
FROM table1 t1
INNER JOIN table2 t2
ON t1.HomeTeam = t2.HomeTeam AND t1.AwayTeam = t2.AwayTeam;
如果您还需要实际的 a
值,那么工作量会增加很多,尤其是考虑到您的数据未标准化。对于比上述更复杂的任何事情,您可能想要规范化数据。