SQL连接不能正常工作

时间:2017-07-15 12:58:29

标签: mysql sql

我有两张桌子

洛杉矶表

+-------------+---------+---------+-----+
| player_name | user_id | tour_id | los |
+-------------+---------+---------+-----+
| P1          |       1 |       3 |   1 |
| P2          |       1 |       3 |   3 |
| P3          |       1 |       3 |   2 |
| P4          |       1 |       3 |   2 |
| P5          |       1 |       3 |   2 |
| P6          |       1 |       3 |   1 |
| P7          |       1 |       3 |   1 |
| P8          |       1 |       3 |   0 |
+-------------+---------+---------+-----+

赢表

+-------------+---------+---------+-----+
| player_name | user_id | tour_id | win |
+-------------+---------+---------+-----+
| P1          |       1 |       3 |   2 |
| P2          |       1 |       3 |   0 |
| P3          |       1 |       3 |   1 |
| P4          |       1 |       3 |   1 |
| P5          |       1 |       3 |   1 |
| P6          |       1 |       3 |   2 |
| P7          |       1 |       3 |   2 |
| P8          |       1 |       3 |   3 |
+-------------+---------+---------+-----+

我想要玩家姓名,Los From First,Win From Second

我希望按玩家名称分组,但它会给出错误的结果

select player_name, los.los, win.win
from los,win
group by player_name;

1 个答案:

答案 0 :(得分:-2)

每当你加入时,必须有一个加入条件,否则它会做一个笛卡尔积。

select los.player_name, sum(los.los), sum(win.win)
from los,win
where los.player_name = win.player_name
group by los.player_name;