【问题标题】:Delete from multiple tables based on date根据日期从多个表中删除
【发布时间】:2018-10-11 09:01:11
【问题描述】:

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

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 的行?

【问题讨论】:

    标签: mysql inner-join sql-delete


    【解决方案1】:

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

    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 中删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2021-08-21
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多