时间:2021-07-01 10:21:17 帮助过:72人阅读
create temporary table car (pinpai varchar(10));insert into car values ('Lambo'),('Audi'),('BMW'),('Audi'),('Toyota'),('Lambo');select pinpai, count(*) as cnt from car group by 1
pinpai cnt Audi 2 BMW 1 Lambo 2 Toyota 1
你没有理解我的意思,你这不是我要的结果,我是要显示 前台显示的是:"现共有4种品牌车辆,共计6辆。" 而不是 把它们每个品牌有多少个显示出来
create temporary table car (pinpai varchar(10));insert into car values ('Lambo'),('Audi'),('BMW'),('Audi'),('Toyota'),('Lambo');select pinpai, count(*) as cnt from car group by 1
pinpai cnt Audi 2 BMW 1 Lambo 2 Toyota 1你没有理解我的意思,你这不是我要的结果,我是要显示 前台显示的是:"现共有4种品牌车辆,共计6辆。" 而不是 把它们每个品牌有多少个显示出来
使用group by + count(*)就可以了.
select pinpai, count(*) as num from car group by pinpai;
select count(*) pinpai, sum(cnt) as cnt from (select pinpai, count(*) as cnt from car group by 1) T
pinpai cnt 4 6
select count(*) pinpai, sum(cnt) as cnt from (select pinpai, count(*) as cnt from car group by 1) T
pinpai cnt 4 6