我想用这是我的表格选择 3 个最大的销售记录
CREATE TABLE IF NOT EXISTS `contas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_service` int(11) DEFAULT NULL,
`data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;
'id_service'是主列,销售额越多,添加相同'id_service'的记录越多。
那么我该如何在不使用 PHP 的情况下按降序进行选择呢?
我试过了
select id_service, count(*) as id_service
from vendas WHERE id_service is not null
group by id_service order by id_service desc LIMIT 3
最佳答案
您已将两列别名为相同的名称。难怪查询很困惑。试试这个:
select id_service, count(*) as cnt
from vendas WHERE id_service is not null
group by id_service
order by cnt desc
LIMIT 3;
关于php - 选择 3 件销量较高的商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21162377/