MySQL分类统计后纵表转横表

编程教程 > Java (1844) 2024-11-26 14:39:04

前言

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


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

怎么实现的呢?
如下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函数

 

评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
MySQL分类统计后纵表转横表
​mysql update 将一个表某字段设为另一个表某字段的值例如:将tableA表的column1字段值设置为tableB表 column2字段的值,条件为tableB中的aId字段等于ta...
MySQL查询中null转0
前言       作为开发人员,我们经常遇到必须处理数据库相关内容的情况; 通常,当客户端以excel工作表的形式向您发送其数据,并且在经过一些excel操作后将数据推送到数据库表时,便完成了该操作
MyBatis generator 表名/字段名下划线转驼峰,Spring Boot 2.0 整合MyBatis generator 表名/字段名下划线转驼峰 idea上的配置
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...
背景最近有些数据需要处理下,用程序来跑也简单,但是想着能否直接通过数据库来处理。就有了本文的MySQL replace 字符串替换函数使用教程了。MySQL replace 使用参考语句:UPD...
MySQL分页_MySQL分页语句_MySQ如何使用limit分页,本博客将会详细讲解mysql中的分页查询。讲清楚MySQL如何编写分页查询语句。
spring boot 2.0 security 5.0 整合,实现自定义表单登录。spring boot 2.0框架使用。
MySQL慢查询优化_MySQL慢查询排查_MySQL慢查询设置配置