我的表结构如下
现在,我的要求是我需要查询交易最多的供应商
SELECT vendor_id,COUNT(deal_id) AS total_dealid
FROM `user_downloaded_deals`
GROUP BY `vendor_id`
但是它按vendor_id顺序返回数据,而我需要按total_dealid
ORDER来返回数据。表示下载最多的供应商的交易首先显示
例如当前的o / p是
vendor_id total_dealid
1 3
2 2
5 5
预期O / p
vendor_id total_dealid
5 5
1 3
2 2
答案 0 :(得分:1)
尝试此查询
SELECT vendor_id,COUNT(deal_id) AS total_dealid
FROM `user_downloaded_deals`
GROUP BY `vendor_id` ORDER BY total_dealid DESC
答案 1 :(得分:0)
使用此查询
SELECT vendor_id,COUNT(deal_id) AS total_dealid FROM user_downloaded_deals
GROUP BY vendor_id order by total_dealid desc
答案 2 :(得分:0)
排序依据也可以与别名一起使用,因此查询如下:
SELECT vendor_id, COUNT(deal_id) AS total_dealid FROM `user_downloaded_deals`
GROUP BY `vendor_id` ORDER BY total_dealid DESC
答案 3 :(得分:0)
您无法在total_dealid
前下订单。在SQL中,无法按别名列进行排序。相反,您必须重复您的订单对帐单。以下应该可以解决您的问题。
SELECT vendor_id,COUNT(deal_id) AS total_dealid
FROM `user_downloaded_deals`
GROUP BY `vendor_id`
ORDER BY count(deal_id) DESC;
编辑:较新的SQL版本允许使用别名列进行排序,因此这也应该起作用:
SELECT vendor_id,COUNT(deal_id) AS total_dealid
FROM `user_downloaded_deals`
GROUP BY `vendor_id`
ORDER BY total_dealid DESC;
答案 4 :(得分:0)
您可以按total_dealid或count(deal_id)desc添加订单
SELECT vendor_id,COUNT(deal_id) AS total_dealid
FROM `user_downloaded_deals`
GROUP BY `vendor_id` order by total_dealid Desc