MYSQL统计请教

wfkmu 2016-09-13 12:33:09
把分类转成列,然后进行统计,哪位有时间帮我看看怎么写这个SQL呀。分类那一列是动态的
支付方式 分类 单价
现金 A 1
现金 B 2
微信 B 3
微信 B 4
微信 A 5
支付宝 A 6
支付宝 C 7

结果:
支付方式 A B C 总价
现金 1 2 0 3
微信 5 7 0 12
支付宝 6 0 7 13
...全文
291 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2016-09-13
  • 打赏
  • 举报
回复
http://blog.csdn.net/acmain_chm/article/details/4283943 MySQL交叉表 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义。http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4cf5-bc1a-e8914aaee5ab.html?96198现整理解法如下:数据样本: create table tx( id int primary key, c1 c...
shoppo0505 2016-09-13
  • 打赏
  • 举报
回复
中文用拼音代替了: with tb (zffs, fl, dj) as ( select 'xj', 'A', 1 union all select 'xj', 'B', 2 union all select 'wx', 'B', 3 union all select 'wx', 'B', 4 union all select 'wx', 'A', 5 union all select 'zfb', 'A', 6 union all select 'zfb', 'C', 7 ) select zffs , (select ISNULL(SUM(dj),0) from tb where fl= 'A' AND zffs = t.zffs) as A , (select ISNULL(SUM(dj),0) from tb where fl= 'B' AND zffs = t.zffs) as B , (select ISNULL(SUM(dj),0) from tb where fl= 'C' AND zffs = t.zffs) as C , (select ISNULL(SUM(dj),0) from tb where zffs = t.zffs) as zj from tb t group by zffs

56,678

社区成员

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

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