zoukankan      html  css  js  c++  java
  • mysql 行转列

    新建表

    CREATE TABLE `data_source`  (
      `Id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      `TableName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `Key` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      `Value` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      PRIMARY KEY (`Key`, `Id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

    插入数据

    INSERT INTO `data_source` VALUES ('1', 'patient', 'address', '南疆路71号');
    INSERT INTO `data_source` VALUES ('5', 'patient', 'address', '离厂路98号');
    INSERT INTO `data_source` VALUES ('1', 'sequence', 'age', '18');
    INSERT INTO `data_source` VALUES ('2', 'sequence', 'age', '26');
    INSERT INTO `data_source` VALUES ('1', 'patient', 'city', '成都');
    INSERT INTO `data_source` VALUES ('2', 'patient', 'city', '北京');
    INSERT INTO `data_source` VALUES ('3', 'patient', 'city', '北京');
    INSERT INTO `data_source` VALUES ('4', 'patient', 'city', '上海');
    INSERT INTO `data_source` VALUES ('5', 'patient', 'city', '深圳');
    INSERT INTO `data_source` VALUES ('6', 'patient', 'city', '南京');
    INSERT INTO `data_source` VALUES ('7', 'patient', 'city', '西安');
    INSERT INTO `data_source` VALUES ('8', 'patient', 'city', '杭州');
    INSERT INTO `data_source` VALUES ('1', 'sequence', 'name', '张三');
    INSERT INTO `data_source` VALUES ('2', 'sequence', 'name', '李思');
    INSERT INTO `data_source` VALUES ('2', 'patient', 'plate', '川A87WQ1');
    INSERT INTO `data_source` VALUES ('3', 'patient', 'plate', '京A2125C');
    INSERT INTO `data_source` VALUES ('1', 'sequence1', 'rote', '18.54');
    INSERT INTO `data_source` VALUES ('2', 'sequence1', 'rote', '26.64');
    INSERT INTO `data_source` VALUES ('1', 'sequence', 'sex', '18');
    INSERT INTO `data_source` VALUES ('2', 'sequence', 'sex', '2');
    INSERT INTO `data_source` VALUES ('1', 'sequence1', 'unit', '');
    INSERT INTO `data_source` VALUES ('2', 'sequence1', 'unit', '');
    INSERT INTO `data_source` VALUES ('1', 'sequence1', 'vg', '1');
    INSERT INTO `data_source` VALUES ('2', 'sequence1', 'vg', '2');

    新建表

    CREATE TABLE `patient`  (
      `Id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      `Name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
      PRIMARY KEY (`Id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

    插入数据

    INSERT INTO `patient` VALUES ('1', '张三');
    INSERT INTO `patient` VALUES ('2', '李四');
    INSERT INTO `patient` VALUES ('3', '王五');
    INSERT INTO `patient` VALUES ('4', '李力');
    INSERT INTO `patient` VALUES ('5', '王达');
    INSERT INTO `patient` VALUES ('6', '名扬');
    INSERT INTO `patient` VALUES ('7', '宋交明');
    INSERT INTO `patient` VALUES ('8', '王文浩');

    普通查询

    SELECT a.*,c.`data` FROM patient AS a
    INNER JOIN
    (
    SELECT
        b.`Id`,
            CONCAT('{',GROUP_CONCAT(""",b.`key`,"":"",b.`Value`,"""),'}') AS `data`
    FROM data_source AS b WHERE b.TableName ='patient' GROUP BY b.Id ) AS c
    ON a.Id=c.Id;

     带条件查询

    SELECT a.id AS Id,a.`data` AS `Data`,b.`value` AS `Value` FROM
    (
    SELECT
        b.`Id`,
        CONCAT('{',GROUP_CONCAT(""",b.`key`,"":"",b.`Value`,"""),'}') AS `data`
    FROM data_source AS b WHERE b.TableName ='sequence' AND b.Id IN(SELECT Id FROM  data_source WHERE  `Key` = 'age' AND `Value` = '18') GROUP BY b.Id) AS a
    INNER JOIN sequence AS b on a.Id=b.`Name`;

  • 相关阅读:
    java request.getInputStream中文乱码解决方案
    MySql5.7配置文件my.ini 设置 my.ini文件路径
    PostgreSQL抛错“不良的类型值: long”之解决
    PostgreSQL-JDBC疑似bug:部分接口参数的表名、列名必须全部小写
    PostgreSQL的空串、空值对唯一性约束的影响
    JPA/Hibernate移植到PostgreSQL时关于CLOB, BLOB及JSON类型的处理
    PostgreSQL函数自动Commit/Rollback所带来的问题
    JBoss EAP 为应用项目配置PostgreSQL数据源
    PostgreSQL 9.4使用UUID
    PostgreSQL视图使用特殊名称作字段时的处理
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/13427712.html
Copyright © 2011-2022 走看看