MySQL从所有表格中删除
我至少有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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要子查询,只需直接在条件下直接测试日期即可。
您需要使用
左JOIN
而不是内部加入
,以防其他表中没有相关行。另外,如果您配置
客户
和订单
使用forefer键和在删除cascade上,则只需要从cdelete - 其他表中相关的行将自动删除。
You don't need the subquery, just test the date directly in the
WHERE
condition.You need to use
LEFT JOIN
rather thanINNER JOIN
in case there are no related rows in the other tables.Also, if you configure
customers
andorders
with a foreign key andON DELETE CASCADE
then you only need to delete fromcdelete
-- the related rows in the other tables will be deleted automatically.