在结果中按最小值排序,同时尊重mysql中的分组

时间:2009-11-13 23:54:20

标签: sql mysql

关于同一主题的两天内的第二个问题。

我目前正在使用以下查询:

SELECT name,suite,webpagetest.id,MIN(priority) AS min_pri
FROM webpagetest,comparefileerrors
WHERE vco="aof" AND user="1" AND calibreversion="9" 
AND webpagetest.id=comparefileerrors.id 
AND comparefileerrors.priority IS NOT NULL 
GROUP BY id,suite 
ORDER BY COALESCE(suite,name),min_pri 
ASC ;

这给我的结果如下:

+-----------------------------+-----------------------------+-------+---------+
| name                        | suite                       | id    | min_pri |
+-----------------------------+-----------------------------+-------+---------+
| bz1273_cmdline_execplussvdb | NULL                        |  6203 |       2 | 
| bz1508_SEGV_password        | NULL                        |  6185 |       2 | 
| bz1747_bad_lvsf             | NULL                        | 36683 |       1 | 
| set_get_status              | shortsRepairDB_2009.1_suite |  6193 |       0 | 
| u2uDemo                     | shortsRepairDB_2009.1_suite |  6195 |       0 | 
| change_sets                 | shortsRepairDB_2009.1_suite |  6194 |       0 | 
| add_delete_mask_polygon     | shortsRepairDB_2009.1_suite |  6191 |       0 | 
| isolate_shorts              | shortsRepairDB_2009.1_suite |  6196 |       0 | 
| add_delete_text             | shortsRepairDB_2009.1_suite |  6197 |       0 | 
| assign_short_AND_user_info  | shortsRepairDB_2009.1_suite |  6198 |       2 | 
| comment_short               | shortsRepairDB_2009.1_suite |  6192 |       2 | 
+-----------------------------+-----------------------------+-------+---------+

但是,我想要做的是按照套件中遇到的最低优先级(如果有的话)对它们进行排序。 Suite是一个可选字段,如果为null,则无需进行此分组。我想使用套件中的最小值来确定套件的整体位置。

我正在考虑重新设计我的应用程序以使用PHP来进行这种排序,但同时使用mysql这样做会很好。

结果应如下所示:

+-----------------------------+-----------------------------+-------+---------+
| name                        | suite                       | id    | min_pri |
+-----------------------------+-----------------------------+-------+---------+
| set_get_status              | shortsRepairDB_2009.1_suite |  6193 |       0 | 
| u2uDemo                     | shortsRepairDB_2009.1_suite |  6195 |       0 | 
| change_sets                 | shortsRepairDB_2009.1_suite |  6194 |       0 | 
| add_delete_mask_polygon     | shortsRepairDB_2009.1_suite |  6191 |       0 | 
| isolate_shorts              | shortsRepairDB_2009.1_suite |  6196 |       0 | 
| add_delete_text             | shortsRepairDB_2009.1_suite |  6197 |       0 | 
| assign_short_AND_user_info  | shortsRepairDB_2009.1_suite |  6198 |       2 | 
| comment_short               | shortsRepairDB_2009.1_suite |  6192 |       2 | 
| bz1747_bad_lvsf             | NULL                        | 36683 |       1 | 
| bz1273_cmdline_execplussvdb | NULL                        |  6203 |       2 | 
| bz1508_SEGV_password        | NULL                        |  6185 |       2 | 
+-----------------------------+-----------------------------+-------+---------+

2 个答案:

答案 0 :(得分:0)

您可以将套件的最低优先级添加为列。假设套件在网页测试中并且在comparefileerrors中具有优先级,例如:

SELECT name,suite,webpagetest.id, MIN(priority) AS min_pri,
    (select min(wt2.priority) 
     from webpagetest wt2 
     inner join comparefileerrors cfe2
         on wt2.id = cfe2.id
     where wt2.suite = wt.suite) as suite_min_pri
FROM webpagetest wt, comparefileerrors cfe

然后按顺序使用它:

ORDER BY COALESCE(suite,name), suite_min_pri 

答案 1 :(得分:0)

如果[{1}}的值在[0,9]范围内,则可以为组合优先级添加另一列,并将该值设为10 *(如果套件存在?1:0)+ priority < / p>

priority

如果优先级在更大的数字集中,则只需增加乘数。关键是要比最大优先级值大一位数。