查询结果扩展字段,这个查询如何做?

okdns 2010-08-23 06:01:41
数据表范例:

date type
---------------------
2010-08-05 a
2010-08-05 a
2010-08-05 a
2010-08-05 b
2010-08-05 c
2010-08-05 c
2010-08-06 a
2010-08-06 b
2010-08-06 c
2010-08-06 c


要求查询结果样式:

date count(type=a) count(type=b) count(type=c)
---------------------------------------------------------------
2010-08-05 3 1 1
2010-08-06 1 1 2



...全文
39 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
okdns 2010-08-24
  • 打赏
  • 举报
回复
谢谢两位,学到了
feixianxxx 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acmain_chm 的回复:]

SQL code
select `date`,
sum(if(`type`='a',1,0)) as `count(type=a)`,
sum(if(`type`='b',1,0)) as `count(type=b)`,
sum(if(`type`='c',1,0)) as `count(type=c)`

from 数据表范例
group by `date`……
[/Quote]
..就是这样了

LZ这是典型行专列 应该掌握的..
技巧就是用分组加聚合的形式
ACMAIN_CHM 2010-08-23
  • 打赏
  • 举报
回复
另外注意 date, type 均为保留字,在创建表的时候尽量避免使用保留字。
ACMAIN_CHM 2010-08-23
  • 打赏
  • 举报
回复
select `date`,
sum(if(`type`='a',1,0)) as `count(type=a)`,
sum(if(`type`='b',1,0)) as `count(type=b)`,
sum(if(`type`='c',1,0)) as `count(type=c)`

from 数据表范例
group by `date`

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧