基于日期从多个表中删除

kqlmhetl  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(307)

我正在尝试从3个表中删除具有相同id的记录。table是这样的:

Table1
+----------+----------------+-----------+---------+----------+
| commonid | creation_date  | column 1  | column 2| column 3 |
+----------+----------------+-----------+---------+----------+

Table2
+----------+---------+----------+---------+----------+
| commonid | column 1| column 2 | column 3| column 4 |
+----------+---------+----------+---------+----------+

Table3
+----------+---------+----------+---------+----------+
| commonid | column 1| column 2 | column 3| column 4 |
+----------+---------+----------+---------+----------+

所以要选择我使用的所有数据

SELECT * FROM table1
INNER JOIN table2 
ON table1.commonid = table2.commonid 
INNER JOIN table3
ON table1.commonid = table3.commonid
WHERE creation_date = '2018-08-01 04:13:50'

返回6行。要删除,我将尝试:

DELETE table1 FROM table1
INNER JOIN table2 
ON table1.commonid = table2.commonid 
INNER JOIN table3
ON table1.commonid = table3.commonid
WHERE creation_date = '2018-08-01 04:13:50'

返回1行,我本以为是6行。当我再次运行第一个查询时,得到0个结果。表3的总行数不受影响。
如何删除具有相同属性的行 commonid 从每张table上?

yftpprvb

yftpprvb1#

您需要将所有三个表都指定为删除目标:

DELETE t1, t2, t3
FROM table1 t1
INNER JOIN table2 t2
    ON t1.commonid = t2.commonid 
INNER JOIN table3 t3
    ON t1.commonid = t3.commonid
WHERE
    creation_date = '2018-08-01 04:13:50';

目前,您只告诉mysql从 table1 .

相关问题