mysql乘法返回错误结果

yrefmtwq  于 2021-07-27  发布在  Java
关注(0)|答案(2)|浏览(486)

我有两个外键相关的表。员工表如下:

  1. +----+------------+-----------+---------------+
  2. | id | first_name | last_name | billable_rate |
  3. +----+------------+-----------+---------------+
  4. | 1 | James | Maxston | 300 |
  5. | 2 | Sean | Scott | 500 |
  6. +----+------------+-----------+---------------+

时间表如下:

  1. +----+----------+------------+------------+----------+-------------+
  2. | id | project | date | start_time | end_time | employee_id |
  3. +----+----------+------------+------------+----------+-------------+
  4. | 1 | AIT | 2020-07-20 | 09:00:00 | 12:00:00 | 1 |
  5. | 2 | Axiiscom | 2020-06-20 | 15:00:00 | 17:00:00 | 1 |
  6. | 3 | AIT | 2020-07-20 | 13:00:00 | 18:00:00 | 1 |
  7. | 4 | AIT | 2020-07-01 | 11:00:00 | 14:00:00 | 2 |
  8. | 5 | AIT | 2020-06-21 | 11:00:00 | 12:00:00 | 2 |
  9. +----+----------+------------+------------+----------+-------------+

运行以下查询:

  1. SELECT
  2. project, employee_id, @hours_worked := SUM(timestampdiff(HOUR, start_time, end_time)) AS number_of_hours,
  3. @hourly_rate :=my_db.employee.billable_rate AS unit_price,
  4. @hours_worked * @hourly_rate AS cost
  5. FROM
  6. my_db.timesheet
  7. INNER JOIN
  8. my_db.employee ON my_db.employee.id = my_db.timesheet.employee_id
  9. WHERE
  10. project = "AIT"
  11. GROUP BY
  12. employee_id;

产生以下结果:

  1. +---------+-------------+-----------------+------------+-------------------------------------+
  2. | project | employee_id | number_of_hours | unit_price | cost |
  3. +---------+-------------+-----------------+------------+-------------------------------------+
  4. | AIT | 1 | 8 | 300 | 1200.000000000000000000000000000000 |
  5. | AIT | 2 | 4 | 500 | 2000.000000000000000000000000000000 |
  6. +---------+-------------+-----------------+------------+-------------------------------------+

相反,我期望它会产生这样的结果:

  1. +---------+-------------+-----------------+------------+-------------------------------------+
  2. | project | employee_id | number_of_hours | unit_price | cost |
  3. +---------+-------------+-----------------+------------+-------------------------------------+
  4. | AIT | 1 | 8 | 300 | 2400.000000000000000000000000000000 |
  5. | AIT | 2 | 4 | 500 | 2000.000000000000000000000000000000 |
  6. +---------+-------------+-----------------+------------+-------------------------------------+

我的问题哪里出错了?

htrmnn0y

htrmnn0y1#

在mysql中,您无法控制何时 @variable 在处理查询时更新值。所以要避免使用它们。它们会导致混乱。
您仍然可以使您的业务逻辑(在您的案例中是 cost )相当容易阅读。
尝试使用嵌套查询:类似这样的查询。

  1. SELECT project, employee_id, number_of_hours, unit_price,
  2. unit_price * number_of_hours AS cost
  3. FROM (
  4. SELECT my_db.timesheet.project,
  5. my_db.timesheet.employee_id,
  6. SUM(timestampdiff(HOUR, start_time, end_time)) AS number_of_hours,
  7. my_db.employee.billable_rate AS unit_price
  8. FROM my_db.timesheet
  9. INNER JOIN my_db.employee
  10. ON my_db.employee.id = my_db.timesheet.employee_id
  11. GROUP BY my_db.timesheet.project,
  12. my_db.timesheeet.employee_id,
  13. my_db.employee.billable_rate
  14. ) summary
  15. WHERE project = 'AIT'
  16. ORDER BY employee_id

mysql的queryplanner在处理这种嵌套查询方面做得相当好,因此您不必太担心性能。
如果需要,可以将内部查询定义为视图。那么你的外部查询真的很容易阅读。

  1. SELECT project, employee_id, number_of_hours, unit_price,
  2. unit_price * number_of_hours AS cost
  3. FROM my_db.project_summary
  4. WHERE project = 'AIT'
  5. ORDER BY employee_id

要创建视图,需要

  1. CREATE VIEW my_db.project_summary AS
  2. SELECT my_db.timesheet.project,
  3. my_db.timesheet.employee_id,
  4. SUM(timestampdiff(HOUR, start_time, end_time)) AS number_of_hours,
  5. my_db.employee.billable_rate AS unit_price
  6. FROM my_db.timesheet
  7. INNER JOIN my_db.employee
  8. ON my_db.employee.id = my_db.timesheet.employee_id
  9. GROUP BY my_db.timesheet.project,
  10. my_db.timesheeet.employee_id,
  11. my_db.employee.billable_rate

易读是好的。

展开查看全部
zf9nrax1

zf9nrax12#

不使用变量,一个简单的聚合可以完成您需要的处理。例如,您可以执行以下操作:

  1. select
  2. t.project,
  3. t.employee_id,
  4. sum(timestampdiff(HOUR, t.start_time, t.end_time)) as number_of_hours,
  5. max(e.billable_rate) as unit_price,
  6. sum(timestampdiff(HOUR, t.start_time, t.end_time))
  7. * max(e.billable_rate) as cost
  8. from my_db.timesheet t
  9. join my_db.employee e on e.id = t.employee_id
  10. where t.project = 'AIT'
  11. group by t.project, t.employee_id

相关问题