order by 失效问题
資深大佬 : Leo818 4
SELECT
SUM( IFNULL( r.read_total, 0 ) ) AS ReadCount,
SUM( IFNULL( r.get_total, 0 ) ) AS GetCount
FROM
report_promote_detail AS r
LEFT JOIN pl_promote AS p ON r.promote_id = p.id
WHERE
r.planet_id = 45
AND r.date
GROUP BY
r.promote_id
ORDER BY
ReadCount + GetCount DESC
SUM( IFNULL( r.read_total, 0 ) ) AS ReadCount,
SUM( IFNULL( r.get_total, 0 ) ) AS GetCount
FROM
report_promote_detail AS r
LEFT JOIN pl_promote AS p ON r.promote_id = p.id
WHERE
r.planet_id = 45
AND r.date
GROUP BY
r.promote_id
ORDER BY
ReadCount + GetCount DESC
以上代码,使用两个数的和进行排序结果不对
目前可以有两种方式解决这个问题
1 、
ReadCount + GetCount 换成 SUM( IFNULL( r.read_total, 0 ) ) +SUM( IFNULL( r.get_total, 0 ) )
排序结果是正确的
2 、
SELECT 中 SUM( IFNULL( r.read_total, 0 ) ) +SUM( IFNULL( r.get_total, 0 ) ) AS Total …… ORDER BY Total DESC
这个结果也没问题
请教下这个是什么情况,为啥两个别名相加不行呢?(之前也这样做过,但是可以,猜测是因为 SUM 函数或者 GROUP BY ?)
大佬有話說 (6)