我试图简化一个mysql查询,我没有运气

时间:2014-07-24 04:15:27

标签: mysql

这是我的原始查询

SELECT 
    COUNT(WRK.workorder_id) AS 'count',
    USR.user_name, 
    IF(
        ((COUNT(WRK.workorder_id) * 100) / (SELECT COUNT(workorder_id) FROM mod_workorder_data WHERE status_id < 8 AND assigned_user_id IS NOT NULL) < 50),
        (50- ((COUNT(WRK.workorder_id) * 100) / (SELECT COUNT(workorder_id) FROM mod_workorder_data WHERE status_id < 8 AND assigned_user_id IS NOT NULL))),
        -((COUNT(WRK.workorder_id) * 100) / (SELECT  COUNT(workorder_id) FROM mod_workorder_data WHERE status_id < 8 AND assigned_user_id IS NOT NULL))
    ) AS 'weight'
FROM mod_workorder_data WRK
LEFT JOIN mod_users_data USR ON USR.user_id = WRK.assigned_user_id
WHERE 
    status_id < 8
    AND WRK.assigned_user_id IS NOT NULL
GROUP BY USR.user_name

结果如下:

+-------+-----------+---------+
| count | user_name | weight  |
+-------+-----------+---------+
|     7 | 1:1Sum    | 47.3384 |
|    47 | ahin      | 32.1293 |
|    19 | asutt     | 42.7757 |
|    26 | bwan      | 40.1141 |
|     4 | cperr     | 48.4791 |
|    41 | dbemo     | 34.4106 |
|    41 | derob     | 34.4106 |
|    38 | dmay      | 35.5513 |
|    39 | jsue      | 35.1711 |
|     1 | lmar      | 49.6198 |
+-------+-----------+---------+

所以我认为可以简化查询以减少冗余,所以我提出了这个:

SET @total := (SELECT COUNT(workorder_id) FROM mod_workorder_data WHERE status_id < 8 AND assigned_user_id IS NOT NULL);

SELECT 
    @countIds := COUNT(WRK.workorder_id) AS 'count',
    USR.user_name,
    IF(
        (@countIds * 100) / @total < 50,
        (50 - (@countIds * 100) / @total),
        -(@countIds * 100) / @total
    ) AS 'weight'
FROM mod_workorder_data WRK
LEFT JOIN mod_users_data USR ON USR.user_id = WRK.assigned_user_id
WHERE
    status_id < 8 
    AND WRK.assigned_user_id IS NOT NULL
GROUP BY USR.user_name

结果如下:

+-------+-----------+--------+
| count | user_name | weight |
+-------+-----------+--------+
|     7 | 1:1Sum    |   NULL |
|    47 | ahin      |   NULL |
|    19 | asutt     |   NULL |
|    26 | bwan      |   NULL |
|     4 | cperr     |   NULL |
|    41 | dbemo     |   NULL |
|    41 | derob     |   NULL |
|    38 | dmay      |   NULL |
|    39 | jsue      |   NULL |
|     1 | lmar      |   NULL |
+-------+-----------+--------+

有人可能会以我的方式看到错误吗?我在简化查询中必须有一些逻辑错误,但我不知道是什么??

我在Fedora 18盒子上使用MySql版本5.5.32,如果它有很大的不同。

由于

Dan Bemowski

1 个答案:

答案 0 :(得分:1)

请使用内联视图,而不是correlated subquery

SELECT COUNT(WRK.workorder_id) AS 'count',
    USR.user_name, 
    IF(
        ((COUNT(WRK.workorder_id) * 100) / tab_cnt.cnt < 50),
        (50- ((COUNT(WRK.workorder_id) * 100) / tab_cnt.cnt)),
        -((COUNT(WRK.workorder_id) * 100) / tab_cnt.cnt)
    ) AS 'weight'
FROM mod_workorder_data WRK LEFT JOIN mod_users_data USR ON USR.user_id = WRK.assigned_user_id
    INNER JOIN (
        SELECT COUNT(workorder_id) AS cnt
        FROM  mod_workorder_data WHERE status_id < 8 AND assigned_user_id IS NOT NULL
    ) AS tab_cnt
WHERE status_id < 8
    AND WRK.assigned_user_id IS NOT NULL
GROUP BY USR.user_name;