示例:

表结构如下:

CREATE TABLE `pressure` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`presurename` varchar(10) DEFAULT NULL COMMENT '离型剂',
`liquidpressure` varchar(10) DEFAULT NULL COMMENT '液体压力',
`cumulativeflow` varchar(10) DEFAULT NULL COMMENT '累计流量',
`timetraffic` varchar(10) DEFAULT NULL COMMENT '瞬时流量',
`maximumflow` varchar(10) DEFAULT NULL COMMENT '最高流量',
`systime` datetime DEFAULT NULL COMMENT '系统时间',
`sysday` date DEFAULT NULL COMMENT '日期',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='锻压机内工艺数据记录,离型剂压力流量监控数据保存。';

插入数据

INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (7, 'R3上模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (9, 'R3下模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (10, 'R3上模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (11, 'R3下模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (12, 'R5上模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (13, 'R5下模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (14, 'R5上模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (15, 'R5下模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (17, 'R3上模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (18, 'R3下模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (19, 'R3上模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (20, 'R3下模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (21, 'R5上模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (22, 'R5下模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (23, 'R5上模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');
INSERT INTO `pressure`(`id`, `presurename`, `liquidpressure`, `cumulativeflow`, `timetraffic`, `maximumflow`, `systime`, `sysday`) VALUES (24, 'R5下模腔', '2', '3', '4', '4', '2021-11-09 17:07:19', '2021-11-09');

需求:将表中的pressurename字段的值变为字段,使用存储过程是实现。

sql语句如下:

CREATE DEFINER=`root`@`%` PROCEDURE `getpressuredays`()
BEGIN


set @day :=7;

Select

group_concat(

DISTINCT

if(liquidpressure is null,

CONCAT('max(if (presurename is null, presurename, 0)) as ''NULL'' '),

CONCAT('max(if (presurename=''', presurename, ''', liquidpressure, ''-'')) as ''',presurename, ''' '))

) into @sql from pressure join (SELECT @sql:='')a;

set @sql = concat('select DATE_FORMAT(sysday,"%Y-%m-%d") as systime, ', @sql, 'from pressure
where DATE_SUB(CURDATE(), INTERVAL "',@day,'" DAY)
<= date(sysday)
group by DATE_FORMAT(sysday,"%Y-%m%d") order by sysday desc;');

PREPARE stmt FROM @sql;

EXECUTE stmt;

END

验证结果:

mysql 存储国过程实现竖表变横表(将行数据值变为字段)的更多相关文章

  1. sql中纵表变横表

    纵表格式如图所示: 查询sql语句如下: ),content)content,Date from SummerChina ' 变成横表如图所示: 纵表变横表sql语句如下: select Time, ...

  2. sql 语句纵表变横表

    现把转换方法列举如下: 1.纵表转横表: 纵表结构 TableA Name Course Grade 张三 语文 75 张三 数学 80 张三 英语 90 李四 语文 95 李四 数学 55 横表结构 ...

  3. 【转】纵表、横表互转的SQL

    纵表.横表互转的SQL 原文1:http://takkymj.iteye.com/blog/751401   横表就是普通的建表方式,如一个表结构为: 主键.字段1.字段2.字段3... 如果变成纵表 ...

  4. 纵表、横表互转的SQL

    纵表.横表互转的SQL By:大志若愚 1.建表: 纵表结构 Table_A  create table Table_A ( 姓名 ), 课程 ), 成绩 int ) ) ) ) ) ) 姓名 课程 ...

  5. SQL竖表转横表 / 横表转竖表

    竖表转横表 竖表结构: Name Course Grade 张三 语文 75 张三 数学 80 张三 英语 90 李四 语文 95 李四 数学 55 转换后横表结构: Name 语文 数学 英语 张三 ...

  6. [No0000128]SQL纵表与横表互转

    1.纵表转横表: 纵表结构:Table1 转换后的横表结构: Sql示例代码: select username, sum(case Course when '语文' then Grade else 0 ...

  7. SQL Server之纵表与横表互转

    1,纵表转横表 纵表结构 Table_A: 转换后的结构: 纵表转横表的SQL示例: SELECT  Name ,        SUM(CASE WHEN Course = N'语文' THEN G ...

  8. SQL竖表转横表Json数据

    1.数据准备 create  table  Vertical(  Id  int ,  ProjectName varchar(20),  ProjectValue int ) insert into ...

  9. mysql 纵表转横表

    表名:sales SELECT NAME, sum( CASE MONTH WHEN '一月份' THEN money ELSE END ) AS '一月份', sum( CASE MONTH WHE ...

  10. SQL SERVER 竖表变成横表

    现有数据如下: Sql: select a.MODELID, max( case a.PNAME when'计划开始' then a.PVALUE end) as RStart, max( case ...

随机推荐

  1. express的使用:接口的编写(三)

    1.接口的跨域问题 a.CORS,主流 b.JSONP,只支持get请求 步骤:a.安装 npm install cors b.使用  const cors = require('cors') 导入中 ...

  2. Windows 操作

    一.基础命令 1. 盘符切换:D:: 2. 查看当前目录文件:dir: 3. 当前盘符内目录切换:cd path: 二.其他命令 1. netstat:查看网络端口状态 A. 查看某一端口信息:net ...

  3. 20192305 王梓全Python程序设计实验一报告

    20192305 王梓全Python程序设计实验一报告 课程:<Python程序设计> 班级: 1923 姓名: 王梓全 学号:20192305 实验教师:王志强 实验日期:2021年4月 ...

  4. ZSTUOJ刷题⑩:Problem B.--零起点学算法103——查找最大元素

    Problem B: 零起点学算法103--查找最大元素 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 9951  Solved: 4793 Descri ...

  5. Flask默认配置参数

    方式一:字段赋值方式导入 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from flask import Flask   app = Flask(__name__)   app. ...

  6. CSRF跨站点请求伪造(Cross Site Request Forgery)攻击

    CSRF跨站点请求伪造(Cross Site Request Forgery)和XSS攻击一样,有巨大的危害性,就是攻击者盗用了用户的身份,以用户的身份发送恶意请求,但是对服务器来说这个请求是合理的, ...

  7. vins-fusion(1)安装编译

    https://github.com/HKUST-Aerial-Robotics/VINS-Fusion https://blog.csdn.net/haner27/article/details/1 ...

  8. Tacotron2论文阅读笔记

    Tacotron2 NATURAL TTS SYNTHESIS BY CONDITIONING WAVENET ON MEL SPECTROGRAM PREDICTIONS论文阅读笔记 先推荐一篇比较 ...

  9. software Engineering homework 4

    博客信息 沈阳航空航天大学计算机学院2020软件工程作业 作业要求 https://edu.cnblogs.com/campus/sau/Computer1701-1705/homework/1068 ...

  10. Tomcat启动—本地文件夹

    打开tomcat文件夹 打开bin目录 在路径这里输入cmd 就可以直接跳转到当前页面下 接下来我们在cmd命令中启动startup.bat (记得设置java环境变量) 这里我没设置utf-8 我无 ...