我想为以下场景编写一个Mysql查询。
1.检查表(例如:tableA)是否存在。
2.检查表中是否有数据。
3.如果tableA存在并且表中存在数据,则将所有数据移动到另一个表(例如:tableB)(db中存在tableB,并且两个表具有相同的结构)
4.删除表A
是否可以编写一个mysql查询而不使用mysql存储过程?
最佳答案
我可以使用以下查询执行前三个操作,但无法删除表。 希望对您有帮助!
有两个表: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
关于mysql - 将数据从一个表移动到mysql中的另一个表并删除该表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22507188/