以下查询的结果遇到一些问题。
我的意图是显示主机收到的总付款的结果,并根据汇总列(以“已收到的总金额($)”为别名)以降序排列。
Select H.host_name AS 'Host Name',
H.location AS 'District',
AVG(F.host_feedback_rating) AS 'Average Rating',
SUM(P.payment_amount) AS 'Total Amount Received ($)'
FROM booking B, host H, payments P, feedback F
WHERE H.host_id = P.host_id
AND F.booking_code = B.booking_code
AND F.host_id = H.host_id
AND P.payment_date between '2018/01/01' and '2018/12/31'
GROUP BY 1,2
ORDER BY 'Total Amount Received ($)' DESC;
当前结果:
Host Name District Average Rating Total Amount Received ($)
John Detroit 5 275
Leeroy Chicago 5 50
Rinoa Texas 5 225
Sally California 4 45
我曾尝试调整GROUP BY子句,但总收款($)不能以某种方式正确排序...您能对此进行指导吗?
非常感谢您!
答案 0 :(得分:0)
'Total Amount Received ($)'
用单引号('
)包围,因此是常量字符串文字。按照技术进行排序虽然在技术上没有错(如您所见-没有错误),但它毫无意义,因为所有行的常量值都相同。
最简单的方法,恕我直言,将根据列的索引而不是别名进行排序:
ORDER BY 4 DESC
答案 1 :(得分:0)
尝试反引号
...ORDER BY `Total Amount Received ($)` DESC
OR
...Order By 4 DESC
或
...SUM(P.payment_amount) DESC
谢谢:)