合并具有相同ID的两个数据库行

时间:2019-09-08 14:28:21

标签: mysql

嗨,我有以下mysql表 enter image description here

如何合并以红色突出显示的两行?

这是我的存储过程代码:

BEGIN

    SELECT

    GROUP_CONCAT(
    CONCAT("MAX(IF(km_kondomanager_millesimal_table_value_table_id='", km_kondomanager_millesimal_table_value_table_id, "',km_kondomanager_millesimal_table_millesimal_value ,0.00)) AS '", km_kondomanager_millesimal_table_name, "'"), "
"
      )INTO @answers
    FROM (
      SELECT DISTINCT km_kondomanager_millesimal_table_value_table_id, km_kondomanager_millesimal_table_name FROM km_kondomanager_millesimal_table_values INNER JOIN km_kondomanager_millesimal_table
                 ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_table_id = km_kondomanager_millesimal_table. km_kondomanager_millesimal_table_id

        WHERE km_kondomanager_millesimal_table_value_group_id = km_group
    ) A;

    SET @query := 
      CONCAT(
        'SELECT km_kondomanager_millesimal_table_value_building_id AS "Flat ID", km_kondomanager_building_unit_code AS "Building Code", CONCAT_WS(" ", km_user_first_name,  km_user_last_name) AS "Proprietario", ', @answers, 

'FROM km_kondomanager_millesimal_table_values 
INNER JOIN km_kondomanager_building_units
ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_building_id = km_kondomanager_building_units. km_kondomanager_building_unit_id 
LEFT JOIN km_kondomanager_building_unit_owners 
ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_building_id = km_kondomanager_building_unit_owners. km_kondomanager_building_unit_owner_building_unit_id 
LEFT JOIN km_users 
ON km_kondomanager_building_unit_owners . km_kondomanager_building_unit_owner_id = km_users. km_user_id
WHERE km_kondomanager_millesimal_table_value_group_id = ',km_group,' GROUP BY km_kondomanager_millesimal_table_value_building_id, km_user_first_name, km_user_last_name'
      );

    PREPARE statement FROM @query;
    EXECUTE statement;
END

这就是我要实现的目标 enter image description here

2 个答案:

答案 0 :(得分:1)

好的,我的以下代码围绕您的陈述而构建。 我无法测试它,因为我没有任何数据,因此您必须对其进行进一步调试。

但是要点是,您选择Flate ID,MAX建筑代码和GROUP_CONCAT Proprietario GROUP by Flate ID,然后在选择了Flate ID,Building ID和Proprietario而不添加@answer的情况下在其中添加@answer。 变量@ answer2包含来自内部Select的km_kondomanager_millesimal_table_name列的最大值

BEGIN

SELECT 
   GROUP_CONCAT(
     CONCAT
     (
     "MAX
   (
     IF
     (km_kondomanager_millesimal_table_value_table_id='",
       km_kondomanager_millesimal_table_value_table_id, "'
      ,km_kondomanager_millesimal_table_millesimal_value ,0.00
      )
  ) AS '", km_kondomanager_millesimal_table_name, "'
"), "

"
),GROUP_CONCAT(
CONCAT
  (
  "MAX
   (
     '", km_kondomanager_millesimal_table_name, "'
  ) AS '", km_kondomanager_millesimal_table_name, "'
"), "

"
) INTO @answers,@answers2
FROM (
  SELECT 
    DISTINCT km_kondomanager_millesimal_table_value_table_id,
    km_kondomanager_millesimal_table_name 
  FROM 
    km_kondomanager_millesimal_table_values 
    INNER JOIN km_kondomanager_millesimal_table
      ON 
       km_kondomanager_millesimal_table_values.
       km_kondomanager_millesimal_table_value_table_id 
      =
       km_kondomanager_millesimal_table.
       km_kondomanager_millesimal_table_id
    WHERE km_kondomanager_millesimal_table_value_group_id = km_group
  ) A;

SET @query := 
  CONCAT(
    'SELECT "Flat ID",MAX("Building Code"),
      GROUP_CONCAT(Proprietario), ', @answers2, 
    ' FROM 
        (
       SELECT 
       km_kondomanager_millesimal_table_value_building_id AS "Flat ID"
       , km_kondomanager_building_unit_code AS "Building Code"
       , CONCAT_WS(" ", km_user_first_name,  km_user_last_name) AS 
       "Proprietario"', @answers, 
     ' FROM 
       km_kondomanager_millesimal_table_values 
       INNER JOIN km_kondomanager_building_units
       ON km_kondomanager_millesimal_table_values.
          km_kondomanager_millesimal_table_value_building_id 
         = km_kondomanager_building_units.
           km_kondomanager_building_unit_id 
       LEFT JOIN km_kondomanager_building_unit_owners 
         ON km_kondomanager_millesimal_table_values.
            km_kondomanager_millesimal_table_value_building_id
           = km_kondomanager_building_unit_owners.
              km_kondomanager_building_unit_owner_building_unit_id 
       LEFT JOIN km_users 
         ON km_kondomanager_building_unit_owners. 
            km_kondomanager_building_unit_owner_id 
           = km_users km_user_id
      WHERE 
        km_kondomanager_millesimal_table_value_group_id = '
        ,km_group,
        ' GROUP BY 
        km_kondomanager_millesimal_table_value_building_id
        , km_user_first_name
        , km_user_last_name
     ) t1
    GROUP BY "Flat ID";'
  );

PREPARE statement FROM @query;
EXECUTE statement;
END

答案 1 :(得分:0)

好的,所以我相信我已经找到了解决问题的方法,这要归功于@nbk

BEGIN
    SELECT

    GROUP_CONCAT(
    CONCAT("MAX(IF(km_kondomanager_millesimal_table_value_table_id='", km_kondomanager_millesimal_table_value_table_id, "',km_kondomanager_millesimal_table_millesimal_value ,0.00)) AS '", km_kondomanager_millesimal_table_name, "'"), "
"
      )INTO @answers
    FROM (
      SELECT DISTINCT km_kondomanager_millesimal_table_value_table_id, km_kondomanager_millesimal_table_name FROM km_kondomanager_millesimal_table_values INNER JOIN km_kondomanager_millesimal_table
                 ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_table_id = km_kondomanager_millesimal_table. km_kondomanager_millesimal_table_id

        WHERE km_kondomanager_millesimal_table_value_group_id = km_group
    ) A;

    SET @query := 
      CONCAT(
        'SELECT km_kondomanager_millesimal_table_value_building_id AS "Building ID", km_kondomanager_building_unit_code AS "Building Code", group_concat(DISTINCT CONCAT_WS(" ", km_user_first_name,  km_user_last_name)) AS "Proprietario", ', @answers, 
        ' FROM km_kondomanager_millesimal_table_values 
          INNER JOIN km_kondomanager_building_units
                 ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_building_id = km_kondomanager_building_units. km_kondomanager_building_unit_id 
          LEFT JOIN km_kondomanager_building_unit_owners 
                 ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_building_id = km_kondomanager_building_unit_owners. km_kondomanager_building_unit_owner_building_unit_id
                 LEFT JOIN km_users 
                 ON km_kondomanager_building_unit_owners . km_kondomanager_building_unit_owner_id = km_users. km_user_id
          WHERE km_kondomanager_millesimal_table_value_group_id = ',km_group,' GROUP BY km_kondomanager_millesimal_table_value_building_id'
      );

    PREPARE statement FROM @query;
    EXECUTE statement;
END