我的mysql版本如下:【版本不是很低】
server version: 5.0.45-log source distribution
從資料表deltable中把那些值在資料表statistic裡有匹配的記錄全刪除掉
delete deltable from deltable a ,statistic b where b.date=a.date and b.server=a.server and b.`key`=a.`key` and b.subkey=a.subkey;
看看這條語句沒有任何問題吧,就是執行不出來結果
我看了一些官方文件,大多數說法如下:
1、從資料表t1中把那些id值在資料表t2裡有匹配的記錄全刪除掉
delete t1 from t1,t2 where t1.id=t2.id 或delete from t1 using t1,t2 where t1.id=t2.id
2、從資料表t1裡在資料表t2裡沒有匹配的記錄查詢出來並刪除掉
delete t1 from t1 left join t2 on t1.id=t2.id where t2.id is null 或
delete from t1,using t1 left join t2 on t1.id=t2.id where t2.id is null
3、從兩個表中找出相同記錄的資料並把兩個表中的資料都刪除掉
delete t1,t2 from t1 left join t2 on t1.id=t2.id where t1.id=25
注意此處的delete t1,t2 from 中的t1,t2不能是別名
如:delete t1,t2 from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25 在資料裡面執行是錯誤的(mysql 版本不小於5.0在5.0中是可以的)
上述語句改寫成
delete table_name,table2_name from table_name as t1 left jointable2_name as t2 on t1.id=t2.id where table_name.id=25在資料裡面執行是錯誤的(mysql 版本小於5.0在5.0中是可以的)
附:上述語句執行的環境 mysql 4.0.2以上
最後取消別名以後:
mysql>delete from
deltableusing
deltable ,s_tmpwhere deltable.date=s_tmp.date
and deltable.server=s_tmp.server and deltable.`key`=s_tmp.`key` and deltable.subkey=s_tmp.subkey;
query ok, 1630 rows affected (48.51 sec)
mysql> select count(*) from s_tmp;
+----------+
| count(*) |
+----------+
| 1642 |
+----------+
1 row in set (0.00 sec)
是不是涉及到的多表刪除都不能使用別名呢
MYSQL中delete刪除多表資料
1 delete from t1 where 條件 2 delete t1 from t1 where 條件 3 delete t1 from t1,t2 where 條件 4 delete t1,t2 from t1,t2 where 條件 前3者是可行的,第4者不可行。也就是簡單用delete語...
MYSQL中delete刪除多表資料
1 delete from t1 where 條件 2 delete t1 from t1 where 條件 3 delete t1 from t1,t2 where 條件 4 delete t1,t2 from t1,t2 where 條件 前3者是可行的,第4者不可行。也就是簡單用delete語...
MYSQL中delete刪除多表資料
delete刪除多表資料,怎樣才能同時刪除多個關聯表的資料呢?這裡做了深入的解釋 1 delete from t1 where 條件 2 delete t1 from t1 where 條件 3 delete t1 from t1,t2 where 條件 4 delete t1,t2 from t1...