【问题标题】:Move data from one table to other in mysql and drop the table在mysql中将数据从一个表移动到另一个表并删除表
【发布时间】:2014-03-19 13:20:39
【问题描述】:

我想为下面的场景写一个Mysql查询。

1.检查表(例如:tableA)是否存在。

2.检查表中是否有数据。

3.如果tableA存在并且表中存在数据,则将所有数据移动到另一个表(例如:tableB)(db中的tableB并且两个表具有相同的结构)

4.drop tableA

是否可以编写一个mysql查询来避免mysql存储过程?

【问题讨论】:

    标签: mysql sql database


    【解决方案1】:

    我能够使用以下查询完成前三个,但无法删除表。 希望对您有所帮助!

    有两个表:table_1(旧),table_2(新)。两者都有一列“ID”。

    insert into table_2(id)     #inserts into table
    select case when
    (
    select count(*) from table_1 a       #checks if there are any records in the table
    where exists                            #checks if table exists
    (select 1 from table_1 b where a.id=b.id))>0 then
    id
    else 0 end
    from table_1
    

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 2019-10-11
      • 2012-04-07
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多