1. 程式人生 > >關於mysql刪除語句的子查詢問題

關於mysql刪除語句的子查詢問題

今天要刪除letterrelation表中無效的資料,於是找出無效的letterrelationId

select letterRelationId from letterrelation where letterRelationId not in(select distinct letterRelationId from letter);

然後在前面加上delete語句

delete from letter where letterRelationId in(select letterRelationId from letterrelation where letterRelationId not
in(select distinct letterRelationId from letter));

呵呵。報錯了
這裡寫圖片描述

create table table_test as select letterRelationId from letterrelation where letterRelationId not in(select distinct letterRelationId from letter);
delete from letterrelation where letterRelationId in (select letterRelationId from table_test);
drop table table_test;

最後發現完美解決了。但是原因我說一下:

  • 1網上的解釋是說mysql工具不強大,無法做到對某個表進行查詢瞭然後又進行刪除操作,但是oracle可以做到。這樣
    算的話就是mysql不強大了。
  • 2同事說是建立檢視了,檢視我不太瞭解,暫且等我先去研究一下