MySQL从所有表格中删除

发布于 2025-02-09 06:57:07 字数 797 浏览 1 评论 0原文

我至少有30个MySQL表包含数据。我有一个mySQL事件,该事件应该从所有表中删除所有行,其中*。uid =(从'cdelete'中选择uid where netatediff(now(),(),date)> = 1) - &gt- ;结果应为1,因为只有ID 1的客户在delete表中。因此,我想删除uid = 1

我到目前为止尝试过的所有行:

DELETE cdelete, customers, orders 
FROM cdelete 
INNER JOIN customers ON customers.id = cdelete.uid
INNER JOIN orders ON orders.uid = cdelete.uid
WHERE cdelete.uid = (SELECT uid FROM 'cdelete' WHERE DATEDIFF( NOW( ) ,  date ) >=1)

看来这种删除是不可能的,或者我犯了一些错误。.任何想法如何从中删除从从'cdelete'中选择UID的所有表中的所有表中,哪些日期(现在(),date)> = 1)语句?

I have at least 30 MySQL Tables with full of data. I have a MySQL event, which should delete all rows from all tables where the *.uid = (SELECT uid FROM 'cdelete' WHERE DATEDIFF( NOW( ) , date ) >=1) -> The result should be 1, because only the customers with the id 1 is in the delete table. So I want to remove every rows where the uid = 1

What I've tried so far:

DELETE cdelete, customers, orders 
FROM cdelete 
INNER JOIN customers ON customers.id = cdelete.uid
INNER JOIN orders ON orders.uid = cdelete.uid
WHERE cdelete.uid = (SELECT uid FROM 'cdelete' WHERE DATEDIFF( NOW( ) ,  date ) >=1)

It seems that this kind of deletion is impossible, or I made some mistake.. Any idea how to delete from all tables where the uid is retrived from the SELECT uid FROM 'cdelete' WHERE DATEDIFF( NOW( ) , date ) >=1) statement?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2025-02-16 06:57:07

您不需要子查询,只需直接在条件下直接测试日期即可。

DELETE cdelete, customers, orders 
FROM cdelete 
LEFT JOIN customers ON customers.id = cdelete.uid
LEFT JOIN orders ON orders.uid = cdelete.uid
WHERE DATEDIFF( NOW( ) ,  cdelete.date ) >=1

您需要使用左JOIN而不是内部加入,以防其他表中没有相关行。

另外,如果您配置客户订单使用forefer键和在删除cascade上,则只需要从cdelete - 其他表中相关的行将自动删除。

You don't need the subquery, just test the date directly in the WHERE condition.

DELETE cdelete, customers, orders 
FROM cdelete 
LEFT JOIN customers ON customers.id = cdelete.uid
LEFT JOIN orders ON orders.uid = cdelete.uid
WHERE DATEDIFF( NOW( ) ,  cdelete.date ) >=1

You need to use LEFT JOIN rather than INNER JOIN in case there are no related rows in the other tables.

Also, if you configure customers and orders with a foreign key and ON DELETE CASCADE then you only need to delete from cdelete -- the related rows in the other tables will be deleted automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文