根据日期从多个表中删除

时间:2018-10-11 09:01:12

标签: mysql inner-join sql-delete

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

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'

因此要选择我正在使用的所有数据

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'

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

commonid

哪条返回受影响的1行,我本来应该是6。当我再次运行第一个查询时,我得到0个结果。表3的总行不受影响。

如何从每个表中删除具有相同struct UserIdentity { std::string name; int id; }; std::map<std::string, int> g_UserIdentities = { { "Bob", 100 }, { "Jone", 101 }, { "Alice", 102 }, { "Doe", 103 } }; /* * Will be used in a DLL that will export UserIdentity struct * OUT _UserIdentity */ void Ui_export(UserIdentity *_UserIdentity) { for (auto& t : g_UserIdentities) { _UserIdentity->name = t.first; _UserIdentity->id = t.second; } } 的行?

1 个答案:

答案 0 :(得分:3)

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

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