我想选择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
答案 0 :(得分: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;