将复杂的MySQL查询转换为UPDATE语句

时间:2016-11-30 14:23:42

标签: mysql sql

我有一个非常复杂的Mysql查询:

SELECT 
    metabase_field.description, rows_to_copy.description,
    metabase_field.display_name, rows_to_copy.display_name
FROM 
  metabase_field LEFT JOIN (select id as table_id, name as t_name from metabase_table) metabase_field_table ON  metabase_field.table_id=metabase_field_table.table_id,
    (
        SELECT metabase_field.name as name, metabase_field_table.t_name as t_name, metabase_field.display_name as display_name, metabase_field.description as description, metabase_field.special_type as type
        FROM metabase_field
        LEFT JOIN  (select id as table_id, name as t_name, db_id, active, visibility_type from metabase_table) metabase_field_table ON metabase_field.table_id = metabase_field_table.table_id
        LEFT JOIN metabase_database metabase_field_table_database ON metabase_field_table.db_id = metabase_field_table_database.id
        where metabase_field_table.active=1 and metabase_field_table.visibility_type is null and metabase_field_table_database.name = 'Prod'
    ) as rows_to_copy
WHERE
    metabase_field_table.table_id IN (
        SELECT distinct(metabase_table.id) as ids
        from metabase_table
        LEFT JOIN metabase_database metabase_table_database ON metabase_table.db_id = metabase_table_database.id
        where metabase_table_database.name = 'Dev' and metabase_table.active=1 and metabase_table.visibility_type is null
    )
    and metabase_field_table.t_name = rows_to_copy.t_name
    and metabase_field.name = rows_to_copy.name

这会返回一个看起来像这样的表:

+-------------+-------------+--------------+--------------+
| description | description | display_name | display_name |
+-------------+-------------+--------------+--------------+
| NULL        | to copy     | Application  | Application  |

我不想深入细节,我只想将rows_to_copy的copty结果转换为metabase_field字段,这意味着设置:

  

metabase_field.description = rows_to_copy.description,       metabase_field.display_name = rows_to_copy.display_name

我尝试将SELECT更改为UPDATE:

UPDATE
  metabase_field LEFT JOIN (select id as table_id, name as t_name from metabase_table) metabase_field_table ON  metabase_field.table_id=metabase_field_table.table_id,
    (
        SELECT metabase_field.name as name, metabase_field_table.t_name as t_name, metabase_field.display_name as display_name, metabase_field.description as description, metabase_field.special_type as type
        FROM metabase_field
        LEFT JOIN  (select id as table_id, name as t_name, db_id, active, visibility_type from metabase_table) metabase_field_table ON metabase_field.table_id = metabase_field_table.table_id
        LEFT JOIN metabase_database metabase_field_table_database ON metabase_field_table.db_id = metabase_field_table_database.id
        where metabase_field_table.active=1 and metabase_field_table.visibility_type is null and metabase_field_table_database.name = 'Prod'
    ) as rows_to_copy
SET
    metabase_field.description = rows_to_copy.description,
    metabase_field.display_name = rows_to_copy.display_name
WHERE
    metabase_field_table.table_id IN (
        SELECT distinct(metabase_table.id) as ids
        from metabase_table
        LEFT JOIN metabase_database metabase_table_database ON metabase_table.db_id = metabase_table_database.id
        where metabase_table_database.name = 'Dev' and metabase_table.active=1 and metabase_table.visibility_type is null
    )
    and metabase_field_table.t_name = rows_to_copy.t_name
    and metabase_field.name = rows_to_copy.name
;

但是这个查询似乎没有改变任何东西 - 结果保持原样。是否有任何简单的方法使其有效?

2 个答案:

答案 0 :(得分:0)

   Update metabase_field set metabase_field.description=
     rows_to_copy.description, metabase_field.display_name=
    rows_to_copy.display_name  LEFT JOIN (select id as table_id, name as
     t_name from metabase_table) metabase_field_table ON
 metabase_field.table_id=metabase_field_table.table_id, ( SELECT
   metabase_field.name as name, metabase_field_table.t_name as t_name,
     metabase_field.display_name as display_name,metabase_field.description as description, metabase_field.special_type
 as type FROM metabase_field LEFT JOIN (select id as table_id, name as
    t_name, db_id, active, visibility_type from metabase_table)
   metabase_field_table ON metabase_field.table_id =
 metabase_field_table.table_id LEFT JOIN metabase_database
 metabase_field_table_database ON metabase_field_table.db_id =
  metabase_field_table_database.id where metabase_field_table.active=1
   and metabase_field_table.visibility_type is null and
   metabase_field_table_database.name = 'Prod' ) as rows_to_copy WHERE
     metabase_field_table.table_id IN ( SELECT distinct(metabase_table.id)
    as ids from metabase_table LEFT JOIN metabase_database
    metabase_table_database ON metabase_table.db_id =
     metabase_table_database.id where metabase_table_database.name = 'Dev'
     and metabase_table.active=1 and metabase_table.visibility_type is null
     ) and metabase_field_table.t_name = rows_to_copy.t_name and
    metabase_field.name = rows_to_copy.name

试试这个

答案 1 :(得分:0)

经过一些格式化后,我意识到几乎不可能跟踪表名。使用更短和不同的别名来避免错误。我尝试改进代码的其余部分,但工作量太大

我认为问题在于更新。更新表应具有别名MF,并且SET MF.<field>

如果不起作用,请移除WHERE并查看您是否UPDATE。当然只有你可以对这些表进行测试。

如果不起作用,请从单个表开始,每次添加一个连接。

UPDATE  metabase_field MF -- main alias for table to update.
LEFT JOIN (SELECT id as table_id, name as t_name 
           FROM metabase_table) MT1
  ON  MF.table_id = MT1.table_id      
CROSS JOIN ( SELECT MF1.name as name, 
                    MT2.t_name as t_name, 
                    MF1.display_name as display_name,
                    MF1.description as description, 
                    MF1.special_type as type
             FROM metabase_field MF1
             LEFT JOIN  (SELECT id as table_id, 
                                name as t_name, 
                                db_id, active, 
                                visibility_type 
                         from metabase_table) MT2
               ON MF1.table_id = MT2.table_id
             LEFT JOIN metabase_database MD
               ON MT2.db_id = MD.id
             WHERE MT2.active = 1 
               and MT2.visibility_type is null 
               and MD.name = 'Prod'
    ) as rows_to_copy
SET
    MF.description = rows_to_copy.description,
    MF.display_name = rows_to_copy.display_name
WHERE
    metabase_field_table.table_id IN (
        SELECT distinct(metabase_table.id) as ids
        from metabase_table
        LEFT JOIN metabase_database MB
          ON MT2.db_id = MB.id
        WHERE MB.name = 'Dev' 
          and MT2.active=1 
          and MT2.visibility_type is null
    )
    and metabase_field_table.t_name = rows_to_copy.t_name
    and metabase_field.name = rows_to_copy.name
;