MySQL:舍入的和不等于整数

时间:2017-11-18 00:08:21

标签: mysql

我有一个查询,其中我将舍入的和与一个整数进行比较,并且我收到了意外的结果。

SELECT assignments.*
  FROM assignments
  INNER JOIN time_entries
    ON time_entries.assignment_id = assignments.id
  WHERE assignments.organization_id = 2
  AND assignments.allocation_mode = 'fixed'
  AND (fixed_hours is not null)
  HAVING round(sum(time_entries.scheduled_hours)) != round(assignments.fixed_hours);

它会返回fixed_hours 20的作业。该列是一个浮点数。

当我选择该记录匹配的time_entries之和时,我得到20.000000298023224。当我致电round时,我会20

SELECT
  round(sum(scheduled_hours))
  FROM time_entries
  WHERE assignment_id=112869;

SELECT round(fixed_hours) from assignments where id=112869也提供了20

当然select round(20.000000298023224) = round(20);会返回1

那么我的查询有什么错误,该记录正在返回?

1 个答案:

答案 0 :(得分:3)

很好的尝试查询摘要:),但你需要一个小组...

SELECT
      assignments.*
FROM assignments
INNER JOIN (
      SELECT
            assignment_id
          , ROUND(SUM(time_entries.scheduled_hours)) sum_hrs
      FROM time_entries
      GROUP BY
            assignment_id
) te ON assignments.id = te.assignment_id
WHERE assignments.organization_id = 2
AND assignments.allocation_mode = 'fixed'
AND (fixed_hours IS NOT NULL)
AND te.sum_hrs <> ROUND(assignments.fixed_hours)
;