mysql - 如何从连接 4 个表的表中删除

标签 mysql sql

您好,以下是我的 4 张 table 。

client_parent_question :-

+----+------------+------------+---------+-----+------+------+
| id | is_deleted | sort_order | version | cid | pid  | qid  |
+----+------------+------------+---------+-----+------+------+
|  1 |            |          1 |       0 |   1 |    1 |    1 |
|  2 |            |          2 |       0 |   1 |    1 |    2 |
|  3 |            |          3 |       0 |   1 |    1 |    3 |
|  4 |            |          4 |       0 |   1 |    1 |    4 |
|  5 |            |          1 |       0 |   1 |    2 |    7 |
+----+------------+------------+---------+-----+------+------+

mysql> select * from client_parent;

+----+------------+------------+---------+-----+------+
| id | is_deleted | sort_order | version | cid | pid  |
+----+------------+------------+---------+-----+------+
|  1 |            |          1 |       0 |   1 |    1 |
|  2 |            |          2 |       0 |   1 |    2 |
+----+------------+------------+---------+-----+------+
2 rows in set (0.00 sec)

mysql> select * from client_question;

+----+------------+---------+-----+------+------+
| id | is_deleted | version | cid | pqid | qtid |
+----+------------+---------+-----+------+------+
|  1 |            |       0 |   1 |    1 |    1 |
|  2 |            |       0 |   1 |    2 |    4 |
|  3 |            |       0 |   1 |    2 |    4 |
|  4 |            |       0 |   1 |    1 |    1 |
|  5 |            |       0 |   1 |    2 |    4 |
|  6 |            |       0 |   1 |    3 |    4 |
|  7 |            |       0 |   1 |    3 |    4 |
|  8 |            |       0 |   1 |    1 |    1 |
|  9 |            |       0 |   1 |    2 |    4 |
| 10 |            |       0 |   1 |    3 |    4 |
| 11 |            |       0 |   1 |    4 |    4 |
| 12 |            |       0 |   1 |    4 |    4 |
+----+------------+---------+-----+------+------+

mysql> select * from client_question_option;

+----+------------+---------+------+------+
| id | is_deleted | version | cqid | oid  |
+----+------------+---------+------+------+
|  1 |            |       0 |    2 |    1 |
|  2 |            |       0 |    3 |    4 |
|  3 |            |       0 |    6 |    2 |
|  4 |            |       0 |    7 |    3 |
|  5 |            |       0 |   11 |    1 |
|  6 |            |       0 |   12 |    4 |
|  7 |            |       0 |   14 |    1 |
|  8 |            |       0 |   15 |    4 |
+----+------------+---------+------+------+

我只知道client_parent表的cid和pid

我的目标是删除 client_question、client_parent_question 和 client_question_option 中的所有内容

client_question_option中的cqid id是client_question表的id

以下是sqlfiddle

我做到了

DELETE FROM cqo,qo,cpq client_question_option cqo ,client_question cq,client_parent_question ,client_parent cp
WHERE cqo.cqid=cq.id AND cq.pqid=pq.id AND cqo.oid=qo.id AND cq.cid=1  AND cp.pid=1

但这并没有奏效。

最佳答案

您只需将 FROM 关键字移到表别名后面即可。请参阅multiple-table syntax

DELETE cqo, qo, cpq 
FROM client_question_option cqo,
     client_question cq,
     client_parent_question,
     client_parent cp
WHERE cqo.cqid=cq.id AND cq.pqid=pq.id AND cqo.oid=qo.id 
      AND cq.cid=1 AND cp.pid=1;

关于mysql - 如何从连接 4 个表的表中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24176793/

相关文章:

php - 构建 Laravel Web API 时更改表列名称

sql - 将空字符串替换为空值

MySQL - 错误 1054 (42S22) : Unknown column 'sal' in 'having clause'

sql - COPY INTO 带有额外列的雪花表

mysql - 如果 slave 在 Symfony2 中不工作,自动连接到 MySQL master

mysql - 使用 TOP 将 SQL Server 转换为 MySQL 内连接错误

MySQL : Get segmented records between multiple date range

mysql - 读取端延迟和响应能力

sql - 避免 SQL Server 中的 while 循环

java - JIRA 插件多次运行单行代码