在mysql中将数据从一个表移动到另一个表并删除表

时间:2014-03-19 13:20:40

标签: mysql sql database

我想为以下场景编写一个Mysql查询。

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

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

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

4.drop tableA

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

1 个答案:

答案 0 :(得分:0)

我能够使用以下查询执行前三个,但是删除表是不可能的。 希望它有所帮助!

有两个表: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