MySQL聯表查詢及聯表刪除都是經常需要用到的操作,下面對MySQL聯表查詢和聯表刪除都作了詳細的介紹分析,希望對您有所幫助。
MySQL聯表查詢:
reference mysql manul:
3.2.7. SELECT語法13.2.7.1. JOIN語法
13.2.7.2. UNION語法
eg1:·mysql> SELECT t1.name, t2.salary FROM employee AS t1, info AS t2·-> WHERE t1.name = t2.name;eg2:·mysql> SELECT table1.* FROM table1·-> LEFT JOIN table2 ON table1.id=table2.id·-> WHERE table2.id IS NULL;
聯表刪除:
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 join table2_name as t2 on t1.id=t2.id where table_name.id=25 在數據裡面執行是錯誤的MYSQL 版本小於5.0在5.0中是可以的)
MySQL CONVERT函數簡介
Liunx下修改MySQL字符集的方法
帶您了解mysql變量
帶您深入了解MySQL用戶權限
MySQL用戶權限設置方法