2014-03-19 38 views
0

我想爲以下方案編寫Mysql查詢。在mysql中將數據從一個表移動到另一個表中並刪除表

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

2.檢查表中是否有數據。

3.如果存在TABLEA和數據是存在表中的所有數據移動到另一個表(例如:tableB的)(以分貝和兩個表都具有相同的結構tableB的存在)

4.drop TABLEA

是否可以寫一個避免mysql存儲過程的mysql查詢?

回答

0

我能用前面的查詢做前三項,但是drop table是不可能的。 希望它有幫助!

有兩個表格: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 
相關問題