簡體   English   中英

使用MySQL從兩個差異表中刪除行

[英]Delete rows from two differences tables with mySQL

正如標題所解釋的,我想從兩個不同的表中刪除行。

在這種情況下,我要刪除UHL_HotelId等於cus_DVndBgfwSVaZhe所有用戶。

___用戶表

|--------|
| USR_Id |
|--------|
|    100 |
|    102 |
|    103 |
|    104 |
|    105 |
|    106 |
|    107 |
|--------|

___UsersHotelsLink

|--------|------------|--------------------|
| UHL_Id | UHL_UserId | UHL_HotelId        |
|--------|------------|--------------------|
|      1 |        100 | cus_DVndBgfwSVaZhe |
|      2 |        101 | cus_DVndBgfwSVaZhe |
|      3 |        102 | cus_DVndBgfwSVaZhe |
|      4 |        103 | cus_DVndBgfwSVaZhe |
|      5 |        104 | cus_DVndBgfwSVaZhe |
|      5 |        105 | cus_DVndBgfwSVaZhe |
|      6 |        106 | cus_GHbnGhGgu3lbnw |
|      7 |        107 | cus_GHbnGhGgu3lbnw |
|--------|------------|--------------------|

我的嘗試如下:

DELETE a.*, b.* 
FROM ___Users a 
JOIN ___UsersHotelsLink b 
    ON b.UHL_UserId = a.USR_Id 
WHERE a.UHL_HotelId = 'cus_DVndBgfwSVaZhe';

為什么我的查詢不起作用?

假設您可以在兩個表之間使用外鍵關系

ON DELETE CASCADE

它“允許您在從父表中刪除數據時自動從子表中刪除數據”

http://www.mysqltutorial.org/mysql-on-delete-cascade/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM