表table_01 里面有两个字段 col1,col2,分别代表“店铺”、“购买时间”我想按店铺统计,本月购买的人当中,是2018和2017年老顾客的人数
- select b.count as 2018年人数, c.count as 2017年人数, a.col1 as 店铺 from table_01 aleft join (select count(1) as count, a.col1 as col1 from table_01 awhere exists (select 1 from table_01 b where a.id = b.id and date_format(a.col2,'%Y') = '2018') group by a.col1) bon a.col1 = b.col1left join (select count(1) as count, a.col1 as col1 from table_01 awhere exists (select 1 from table_01 b where a.id = b.id and date_format(a.col2,'%Y') = '2017') group by a.col1) con a.col1 = c.col1group by a.col1
复制代码请问我这种写法可以吗?有没有更简洁的写法?
![](https://img02.hi-pda.com/forum/images/smilies/default/lol.gif)
我想要的效果是这样的
![](https://img02.hi-pda.com/forum/images/default/attachimg.gif)
![11823978-EBBE-46b1-961F-8A3F4F74560E.png](https://img02.hi-pda.com/forum/attachments/day_190610/1906101326c14237c983a24479.png)