首页> 文章> 详情

MySQL分类统计后纵表转横表

教程分享 > Java教程 (1552) 2024-04-17 12:50:55

前言

很多时候我们使用数据库的分类统计函数后都会得到一个纵向的表
如下:
分类统计后的纵表
分类统计后的纵表


这个时候我们可能需要的是一张横表
如下:
我们想要的横表
我们想要的横表

怎么实现的呢?
如下sql:
select 
	max(case name when '授权' then num end) as 'fmzlsq',
	max(case name when '撤回' then num end) as 'fmzlch',
	max(case name when '实审' then num end) as 'fmzlss',
	max(case name when '公开' then num end) as 'fmzlgk',
	max(case name when '终止' then num end) as 'fmzlzz'
	from (
			select count(a.credit_code) num,a.patent_legal_status name
				from enterprise_patent a 
				where  a.credit_code='91110108551385082Q' and a.patent_type='fmzl'  
				group by a.patent_legal_status,a.credit_code
			) b

可用max/sum函数

 
https://www.leftso.com/article/526.html

相关文章
MySQL分类统计后纵表转横表
sql server 2016数据库导出表数据/表结构
​mysql update 将一个表某字段设为另一个表某字段的值例如:将tableA表的column1字段值设置为tableB表 column2字段的值,条件为tableB中的aId字段等于ta...
MySQL查询中null转0
针对MyISAM高并发锁表的解决方案
前言       作为开发人员,我们经常遇到必须处理数据库相关内容的情况; 通常,当客户端以excel工作表的形式向您发送其数据,并且在经过一些excel操作后将数据推送到数据库表时,便完成了该操作
MyBatis generator 表名/字段名下划线转驼峰,Spring Boot 2.0 整合MyBatis generator 表名/字段名下划线转驼峰 idea上的配置
MySQL索引优化,MySQL索引类型,MySQL索引怎么用MySQL索引怎么创建这里将会通过一些简单得sql进行讲解
mysql数据库备份与还原命令_MySQL导出导入数据命令
mysql索引的使用基础知识摘录。
MySQL5.6数据库双机主从热备配置
MySQL5.6配置双机互为主备
表数据准备Table: Subject_Selection ​ Subject   Semester   Attendee --------------------------------- I...
函数使用mysql&gtl; explain SELECT a.custid, b.score, b.xcreditscore, b.lrscore FROM( SELECT DISTINCT ...
步骤① 创建nacos的数据库首先,MySQL创建一个nacos专用的数据库,暂且命名为 nacos吧,如下:导入nacos的初始化SQL,一般该文件存放于 nacos/conf/nacos-m...