mysql 多行合并一行

2023-03-29  本文已影响0人  Rinaloving

多行合并一行

SELECT GROUP_CONCAT(列名) FROM 表名 WHERE 主键  IN('','','')
QQ截图20230330143230.png
REPLACE(group_concat(F_FullName),",","/")
QQ截图20230330144058.png
SELECT F_FullName FROM `base_organize` WHERE F_Id IN( '96240625-934F-490B-8AA6-0BC775B18468','9E998490-40B2-45EF-A82E-E225BF2109E1','407393336282841029') ORDER BY FIELD(F_Id,
'96240625-934F-490B-8AA6-0BC775B18468','9E998490-40B2-45EF-A82E-E225BF2109E1','407393336282841029'
);
QQ截图20230330145022.png
 SELECT REPLACE(GROUP_CONCAT(F_FullName),",","/")  FROM `base_organize` WHERE  EXISTS  (SELECT
    SUBSTRING_INDEX( SUBSTRING_INDEX( processed_data, ',', b.help_topic_id + 1 ), ',',- 1 ) AS F_DivisionName 
FROM
(
SELECT REPLACE
    (
        REPLACE ( REPLACE ( REPLACE (F_DivisionName, '[', '' ), ']', '' ), '"', '' ),
        ' ',
        '' 
    ) processed_data 
FROM
    `mz_person` WHERE F_Id = 121
) temp
JOIN mysql.help_topic  b ON b.help_topic_id < ( LENGTH( temp.processed_data ) - LENGTH( REPLACE ( temp.processed_data, ',', '' ) ) + 1 ) 
WHERE F_Id = SUBSTRING_INDEX( SUBSTRING_INDEX( processed_data, ',', b.help_topic_id + 1 ), ',',- 1 ) ) 
QQ截图20230330191916.png
SELECT REPLACE(GROUP_CONCAT(F_FullName ORDER BY n.NOrder ),",","/") FROM `base_organize` m   RIGHT JOIN (SELECT
    SUBSTRING_INDEX( SUBSTRING_INDEX( processed_data, ',', b.help_topic_id + 1 ), ',',- 1 ) AS F_DivisionName ,b.help_topic_id AS NOrder
FROM
(
SELECT REPLACE
    (
        REPLACE ( REPLACE ( REPLACE (F_DivisionName, '[', '' ), ']', '' ), '"', '' ),
        ' ',
        '' 
    ) processed_data 
FROM
    `mz_person` WHERE F_Id = 121
) temp
JOIN mysql.help_topic  b ON b.help_topic_id < ( LENGTH( temp.processed_data ) - LENGTH( REPLACE ( temp.processed_data, ',', '' ) ) + 1 )) n ON  m.F_Id = n.F_DivisionName
QQ截图20230330192043.png
UPDATE `mz_person` a SET  a.F_ShowDivisionName = (SELECT REPLACE(GROUP_CONCAT(F_FullName ORDER BY n.NOrder ),",","/") FROM `base_organize` m   RIGHT JOIN (SELECT
    SUBSTRING_INDEX( SUBSTRING_INDEX( processed_data, ',', b.help_topic_id + 1 ), ',',- 1 ) AS F_DivisionName ,b.help_topic_id AS NOrder
FROM
(
SELECT REPLACE
    (
        REPLACE ( REPLACE ( REPLACE (F_DivisionName, '[', '' ), ']', '' ), '"', '' ),
        ' ',
        '' 
    ) processed_data 
FROM
    `mz_person` WHERE F_Id = 121
) temp
JOIN mysql.help_topic  b ON b.help_topic_id < ( LENGTH( temp.processed_data ) - LENGTH( REPLACE ( temp.processed_data, ',', '' ) ) + 1 )) n ON  m.F_Id = n.F_DivisionName)
  WHERE a.F_Id = 121;
DELIMITER //
CREATE PROCEDURE updatePersonDivisionName(IN p INT)
BEGIN
  DECLARE total INT DEFAULT 0;
  WHILE p > 103 DO 
      SET total := p;
      SET p := p - 1;
      UPDATE `mz_person` a SET  a.F_ShowDivisionName = (SELECT REPLACE(GROUP_CONCAT(F_FullName ORDER BY n.NOrder ),",","/") FROM `base_organize` m   RIGHT JOIN (SELECT
    SUBSTRING_INDEX( SUBSTRING_INDEX( processed_data, ',', b.help_topic_id + 1 ), ',',- 1 ) AS F_DivisionName ,b.help_topic_id AS NOrder
FROM
(
SELECT REPLACE
    (
        REPLACE ( REPLACE ( REPLACE (F_DivisionName, '[', '' ), ']', '' ), '"', '' ),
        ' ',
        '' 
    ) processed_data 
FROM
    `mz_person` WHERE F_Id = total
) temp
JOIN mysql.help_topic  b ON b.help_topic_id < ( LENGTH( temp.processed_data ) - LENGTH( REPLACE ( temp.processed_data, ',', '' ) ) + 1 )) n ON  m.F_Id = n.F_DivisionName)
  WHERE a.F_Id = total;
END WHILE;
END //
DELIMITER;
CALL updatePersonDivisionName(30050); 

总结

上一篇 下一篇

猜你喜欢

热点阅读