• 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`;

  • 相关阅读:
    php中curl类常用方法封装和详解
    一个简单的PHP的CURL类
    PHP的curl常用的5个例子
    PHP封装CURL扩展
    马老师的WoTou生产消费线程讲解例子
    URL路径设置----第二章:创建和管理内容
    浅谈js设计模式之迭代器模式
    浅谈js设计模式之代理模式
    浅谈js设计模式之策略模式
    浅谈js设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/13427712.html
Copyright © 2020-2023  润新知