在mysql中,我需要为两种情况设置“ORDER BY”:name或mod_1,具体取决于用户在组合框中选择的内容。我的疑问是:
SELECT
name,
SUM(IF(mod = 1, 1, 0)) AS mod_1,
SUM(IF(mod = 2, 1, 0)) AS mod_2,
SUM(IF(mod = 3, 1, 0)) AS mod_3
FROM
DW_RM_Log
WHERE
...
ORDER BY
name (or mod_1 DESC)
是否可以在mysql中执行此操作或者我需要在php中编写代码?或两者兼而有之?
答案 0 :(得分:2)
厌倦了为你做这件事:
$q = "SELECT
name,
SUM(IF(mod = 1, 1, 0)) AS mod_1,
SUM(IF(mod = 2, 1, 0)) AS mod_2,
SUM(IF(mod = 3, 1, 0)) AS mod_3
FROM
DW_RM_Log
WHERE
...
ORDER BY ";
if ($x) {
$q .= "name DESC";
} else {
$q .= "mod_1 DESC";
}
//run $q!