根据2字段条件从表中删除时从另外2个表中删除

时间:2015-11-08 15:12:36

标签: mysql foreign-key-relationship cascade

在MySQL服务器中,我有3个名为user,thing,thingfile的表

user
uid, username, name, location, version

thing
thingid, username,, version

thingfile
thingid, code, version

每个用户都有很多东西,每个东西都有很多东西。

我将从用户表中删除如下:

dnx web

更新: 删除前:   用户:

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

我希望删除在thing和thingfile表中属于thingid的那些记录。没有外键约束是否可能?我有超过10个表,因此很多连接在一个查询中不起作用。

2 个答案:

答案 0 :(得分: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个结果比网格更容易。)

答案 1 :(得分: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)