我有两个外键相关的表。员工表如下:
+----+------------+-----------+---------------+
| id | first_name | last_name | billable_rate |
+----+------------+-----------+---------------+
| 1 | James | Maxston | 300 |
| 2 | Sean | Scott | 500 |
+----+------------+-----------+---------------+
时间表如下:
+----+----------+------------+------------+----------+-------------+
| id | project | date | start_time | end_time | employee_id |
+----+----------+------------+------------+----------+-------------+
| 1 | AIT | 2020-07-20 | 09:00:00 | 12:00:00 | 1 |
| 2 | Axiiscom | 2020-06-20 | 15:00:00 | 17:00:00 | 1 |
| 3 | AIT | 2020-07-20 | 13:00:00 | 18:00:00 | 1 |
| 4 | AIT | 2020-07-01 | 11:00:00 | 14:00:00 | 2 |
| 5 | AIT | 2020-06-21 | 11:00:00 | 12:00:00 | 2 |
+----+----------+------------+------------+----------+-------------+
运行以下查询:
SELECT
project, employee_id, @hours_worked := SUM(timestampdiff(HOUR, start_time, end_time)) AS number_of_hours,
@hourly_rate :=my_db.employee.billable_rate AS unit_price,
@hours_worked * @hourly_rate AS cost
FROM
my_db.timesheet
INNER JOIN
my_db.employee ON my_db.employee.id = my_db.timesheet.employee_id
WHERE
project = "AIT"
GROUP BY
employee_id;
产生以下结果:
+---------+-------------+-----------------+------------+-------------------------------------+
| project | employee_id | number_of_hours | unit_price | cost |
+---------+-------------+-----------------+------------+-------------------------------------+
| AIT | 1 | 8 | 300 | 1200.000000000000000000000000000000 |
| AIT | 2 | 4 | 500 | 2000.000000000000000000000000000000 |
+---------+-------------+-----------------+------------+-------------------------------------+
相反,我期望它会产生这样的结果:
+---------+-------------+-----------------+------------+-------------------------------------+
| project | employee_id | number_of_hours | unit_price | cost |
+---------+-------------+-----------------+------------+-------------------------------------+
| AIT | 1 | 8 | 300 | 2400.000000000000000000000000000000 |
| AIT | 2 | 4 | 500 | 2000.000000000000000000000000000000 |
+---------+-------------+-----------------+------------+-------------------------------------+
我的问题哪里出错了?
2条答案
按热度按时间htrmnn0y1#
在mysql中,您无法控制何时
@variable
在处理查询时更新值。所以要避免使用它们。它们会导致混乱。您仍然可以使您的业务逻辑(在您的案例中是
cost
)相当容易阅读。尝试使用嵌套查询:类似这样的查询。
mysql的queryplanner在处理这种嵌套查询方面做得相当好,因此您不必太担心性能。
如果需要,可以将内部查询定义为视图。那么你的外部查询真的很容易阅读。
要创建视图,需要
易读是好的。
zf9nrax12#
不使用变量,一个简单的聚合可以完成您需要的处理。例如,您可以执行以下操作: