【发布时间】:2014-03-19 13:20:39
【问题描述】:
我想为下面的场景写一个Mysql查询。
1.检查表(例如:tableA)是否存在。
2.检查表中是否有数据。
3.如果tableA存在并且表中存在数据,则将所有数据移动到另一个表(例如:tableB)(db中的tableB并且两个表具有相同的结构)
4.drop tableA
是否可以编写一个mysql查询来避免mysql存储过程?
【问题讨论】:
我想为下面的场景写一个Mysql查询。
1.检查表(例如:tableA)是否存在。
2.检查表中是否有数据。
3.如果tableA存在并且表中存在数据,则将所有数据移动到另一个表(例如:tableB)(db中的tableB并且两个表具有相同的结构)
4.drop tableA
是否可以编写一个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
【讨论】: