示例:

表结构如下:

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. 【C学习笔记】day2-5 求两个数的最大公约数

    #include <stdio.h> int main() { int a, b; int min; scanf_s("%d %d", &a, &b); ...

  2. el-scrollbar 饿了么滚动条不出现 bug

    el-scrollbar 饿了么滚动条不出现 bug 解决方法:el-scrollbar 的直接子元素样式需要设置为 display: inline-block;

  3. -bash: ./mlnxofedinstall: /usr/bin/perl: bad interpreter: No such file or directory

    -bash: ./mlnxofedinstall: /usr/bin/perl: bad interpreter: No such file or directory 解决办法:安装相关的环境即可 输 ...

  4. windows U盘启动装机工具(Ventoy)

    Windows优盘启动装机工具 一.WePE 纯净 WePE 启动盘/ U盘重装系统工具 1.U盘重装系统工具下载地址:https://www.wepe.com.cn/download.html?td ...

  5. DDR内存256M16、512M8含义

    256M16,后面的16是代表数据位数,也可以认为是16个bit.一般一个Byte是8个bit.例如256M4的容量为256*16 bit = 4096Mb = 512MB所以 256M16和 512 ...

  6. ES语法(简)

    1.上编写到怎样安装部署elk,这篇记录怎样简单使用.以便于后期复习使用. 2.登录localhost:5601进入开发者模式. 3.初学担心没有数据可以自己PUT数据比较直观的学习语法.这里可以用网 ...

  7. 一文学会使用pip

    pip介绍 pip的全称:package installer for python,是 Python包管理工具,该工具提供了对Python包的查找.下载.安装.卸载的功能.Python官方的PyPi仓 ...

  8. git 代码托管常用代码

    一.git全局账号登录 (1)全局登录账号和密码 git config --global user.name "xxx" git config --global user.emai ...

  9. 学习笔记||Vue踩过的坑3.0

    11.VUE之const,var,let VUE-var 用var命令声明的变量,是在全局范围内有效的 VUE-let let声明的变量,只是在当前循环的代码块中有效. let不允许在相同的作用域内重 ...

  10. app内打开外部第三方瞎下载链接

    Q:我用cordova开发项目,想在app内跳转外部链接,安装了cordova-plugin-inappbrowser后确实可以跳转,但是跳转的页面有个按钮,原本点击会下载app,现在点击后毫无反应, ...