使用样本数据对单个联接表求和的结果加倍和期望结果的问题。对不起,我现在正在学习MySql-设计可能很糟糕-这就是为什么我想学习如何做得更好,并从这里得到一些建议…员工和帐户表与每个月表定义了一对多的关系…此外,我在每个月表中添加了指向员工和帐户表的外来检查
使用样本数据对单个联接表求和的结果加倍和期望结果的问题。对不起,我现在正在学习MySql-设计可能很糟糕-这就是为什么我想学习如何做得更好,并从这里得到一些建议…员工和帐户表与每个月表定义了一对多的关系…此外,我在每个月表中添加了指向员工和帐户表的外来检查,mysql,pivot-table,Mysql,Pivot Table,使用样本数据对单个联接表求和的结果加倍和期望结果的问题。对不起,我现在正在学习MySql-设计可能很糟糕-这就是为什么我想学习如何做得更好,并从这里得到一些建议…员工和帐户表与每个月表定义了一对多的关系…此外,我在每个月表中添加了指向员工和帐户表的外来检查约束-“名称”两个表中的每个表中的字段都定义为主键。非常感谢您的帮助-我是mysql查询编程的新手,我从来不知道“透视表” +----+-------------+--+ | ID | Name | | +----+------
使用样本数据对单个联接表求和的结果加倍和期望结果的问题。对不起,我现在正在学习MySql-设计可能很糟糕-这就是为什么我想学习如何做得更好,并从这里得到一些建议…员工和帐户表与每个月表定义了一对多的关系…此外,我在每个月表中添加了指向员工和帐户表的外来检查约束-“名称”两个表中的每个表中的字段都定义为主键。非常感谢您的帮助-我是mysql查询编程的新手,我从来不知道“透视表”
+----+-------------+--+
| ID | Name | |
+----+-------------+--+
| 1 | John Doe | |
| 2 | Helen Price | |
| 3 | John Price | |
+----+-------------+--+
+----+---------+--+
| ID | Name | |
+----+---------+--+
| 1 | fisher | |
| 2 | fisher1 | |
+----+---------+--+
+----+-------------+---------+----------+----------+----------+
| ID | Employee | Account | firstday | seconday | thirdday |
+----+-------------+---------+----------+----------+----------+
| 1 | John Doe | fisher | 2 | 4 | 6 |
| 2 | Helen Price | fisher | 1 | 1 | 1 |
| 3 | John Price | fisher1 | 1 | 2 | 1 |
| 4 | John Price | fisher | 1 | 1 | 1 |
+----+-------------+---------+----------+----------+----------+
+----+-------------+---------+-----------+----------+-----------+
| ID | Employee | Account | fourthday | eightday | secondday |
+----+-------------+---------+-----------+----------+-----------+
| 1 | John Doe | fisher | 1 | 4 | 6 |
| 2 | Helen Price | fisher | 1 | 1 | 2 |
| 3 | John Price | fisher1 | 1 | 2 | 1 |
| 4 | John Price | fisher | 1 | 1 | 1 |
+----+-------------+---------+-----------+----------+-----------+
+----+-------------+---------+-----------------+------------------+--------+--+
| ID | Employee | Account | January(totals) | February(totals) | Totals | |
+----+-------------+---------+-----------------+------------------+--------+--+
| 1 | John Doe | fisher | 12 | 11 | 23 | |
| 2 | Helen Price | fisher | 3 | 4 | 7 | |
| 3 | John Price | fisher | 3 | 3 | 6 | |
+----+-------------+---------+-----------------+------------------+--------+--+
| ID | Employee | Account | January(totals) | February(totals) | Totals |
| --- | ----------- | ------- | --------------- | ---------------- | ------ |
| 1 | John Doe | fisher | 12 | 11 | 23 |
| 2 | Helen Price | fisher | 3 | 4 | 7 |
| 3 | John Price | fisher | 3 | 3 | 6 |
SUM(CASE MONTH(date_column) WHEN # THEN amount_column ELSE 0 END) AS MonthTotal
| Employee | Account | Year | January(totals) | February(totals) | Totals |
| ----------- | ------- | ---- | --------------- | ---------------- | ------ |
| Helen Price | fisher | 2020 | 3 | 4 | 7 |
| John Doe | fisher | 2020 | 12 | 11 | 23 |
| John Price | fisher | 2020 | 3 | 3 | 6 |
| John Price | fisher1 | 2020 | 4 | 4 | 8 |