2015-11-08 51 views
-1

在MySQL服務器中刪除,我有3個表被叫用戶的事情,thingfile刪除時,從表中基於2場條件

 
user 
uid, username, name, location, version 

thing 
thingid, username,, version 

thingfile 
thingid, code, version 

每個用戶都有很多事情,每一件事情都有許多thingfiles 。

我會從用戶表中刪除象下面這樣:

delete FROM user a WHERE a.username NOT IN (
SELECT b.by_user 
FROM bp_userlist b) 
and a.version="823" 

更新時間: 前刪除: 用戶:

uid username name location version 
1 abc  abc abcdd O 
2 abc  abc abcdd 823 
3 ddd  ddd dddd 823 

thing 

thingid username version 

1  abc  O 
2  abc  O 
1  abc  823 
2  abc  823 
3  ddd  823 
1  ddd   823 

thingfile 
thingid code version 
1  ee O 
2  eed O 
1  ee  823 
2  eddd 823 
3  fff 823 

後刪除: 用戶:

uid username name location version 
1 abc  abc abcdd O 
3 ddd  ddd dddd 823 

thing 

thingid username version 

1  abc  O 
2  abc  O 
3  ddd  823 
1  ddd   823 


thingfile 
thingid code version 
1  ee O 
2  eed O 
3  fff 823 

我想要那些屬於thingi的記錄東西和東西文件表中的東西也會被刪除。是否有可能沒有外鍵約束?我有超過10個表和許多連接不能在一個查詢中工作。

+0

請分享您想要獲得的輸出以及您已經嘗試過的輸出。 –

+0

如果沒有外鍵約束,則必須包含兩個更多的查詢才能從這兩個表中刪除。否則,您如何期望MySQL知道要刪除哪些行,而不必以某種方式定義關係? –

+0

@RacilHilan我可以根據需要添加儘可能多的查詢!沒有添加外鍵約束 – MT467

回答

0

所以你已經有了第一個查詢。另外兩個表需要兩個查詢。不過,你需要以相反的順序執行三個查詢,像這樣:

DELETE t FROM thingfile t 
INNER JOIN thing a ON a.thingid = t.thingid 
        AND a.version = t.version 
WHERE a.username NOT IN (
     SELECT b.by_user 
     FROM bp_userlist b) 
    AND a.version = '823'; 


DELETE a FROM thing a 
WHERE a.username NOT IN (
     SELECT b.by_user 
     FROM bp_userlist b) 
    AND a.version = '823'; 


DELETE a FROM user a 
WHERE a.username NOT IN (
     SELECT b.by_user 
     FROM bp_userlist b) 
    AND a.version = '823'; 

這裏是一個fiddle(選擇「僅文本結果」,並運行該查詢,在文本中看到3個結果比更容易在網格中)。

+0

爲什麼你需要這麼多的查詢? – Strawberry

+1

@Strawberry他想從3個表中刪除記錄,所以他需要3個查詢。你還能怎麼做?發佈一個答案,如果你有一個。 –

+1

對於他的問題,表結構簡直不好使1查詢工作。 – CurseStacker

0

@RacilHilan我不會假裝,這正好對應到所需的解決方案,但我舉例來說,你可以從幾個表中同時刪除(不使用CASCADE ON DELETE)提出它...

DROP TABLE IF EXISTS user; 

CREATE TABLE user 
(user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY 
,version INT NOT NULL); 

INSERT INTO user VALUES 
(1,0), 
(2,823), 
(3,823); 

DROP TABLE IF EXISTS thing; 

CREATE TABLE thing 
(thing_id INT NOT NULL 
,user_id INT NOT NULL 
,version INT NOT NULL 
); 

INSERT INTO thing VALUES 
(1,1,0), 
(2,1,0), 
(1,1,823), 
(2,1,823), 
(3,2,823), 
(1,2,823); 

DROP TABLE IF EXISTS thing_file; 

CREATE TABLE thing_file 
(thing_id INT NOT NULL 
,version INT NOT NULL 
,PRIMARY KEY (thing_id,version) 
); 

INSERT INTO thing_file VALUES 
(1,0), 
(2,0), 
(1,823), 
(2,823), 
(3,823); 

SELECT * FROM user; 
+---------+---------+ 
| user_id | version | 
+---------+---------+ 
|  1 |  0 | 
|  2 |  823 | 
|  3 |  823 | 
+---------+---------+ 

SELECT * FROM thing; 
+----------+---------+---------+ 
| thing_id | user_id | version | 
+----------+---------+---------+ 
|  1 |  1 |  0 | 
|  2 |  1 |  0 | 
|  1 |  1 |  823 | 
|  2 |  1 |  823 | 
|  3 |  2 |  823 | 
|  1 |  2 |  823 | 
+----------+---------+---------+ 

SELECT * FROM thing_file; 
+----------+---------+ 
| thing_id | version | 
+----------+---------+ 
|  1 |  0 | 
|  1 |  823 | 
|  2 |  0 | 
|  2 |  823 | 
|  3 |  823 | 
+----------+---------+ 

DELETE u,t,f 
    FROM user u 
    JOIN thing t 
    ON t.user_id = u.user_id 
    JOIN thing_file f 
    ON f.thing_id = t.thing_id 
    WHERE 823 IN (u.version,t.version,f.version); 
Query OK, 13 rows affected (0.00 sec) 

SELECT * FROM user; 
+---------+---------+ 
| user_id | version | 
+---------+---------+ 
|  3 |  823 | 
+---------+---------+ 
1 row in set (0.00 sec) 

SELECT * FROM thing; 
Empty set (0.00 sec) 

SELECT * FROM thing_file; 
Empty set (0.00 sec) 
+0

我有大約10張桌子,加入並不是這樣工作的,爲了簡單起見,我只在這裏發佈了3張桌子...... – MT467

+0

@ MT467我們只能試圖回答所問的問題。 – Strawberry

+0

所以現在請更新您的答案,謝謝 – MT467