简体   繁体   English

从多个表中删除多行

[英]Deleting multiple rows from multiple tables MYSQL

I have this query that works fine. 我有这个查询工作正常。 Its deletes records that are old based on current time. 它会删除基于当前时间的旧记录。

$cleanacc_1 = "DELETE FROM $acc_1 
    WHERE `Scheduled` < DATE_SUB(UTC_TIMESTAMP(), INTERVAL 30 SECOND)";

$result = mysql_query($cleanacc_1);

However, there are over 100 tables (accounts) that need deleting and I was wondering if I can combine them into one query. 但是,有超过100个表(帐户)需要删除,我想知道是否可以将它们组合成一个查询。 If possible how? 如果可能的话如何?

No way to do that, AFAIK; AFAIK无法做到这一点; anyways, I don't think it would be a big problem to run 100 queries, assuming you are not running that for each request or so.. 无论如何,我认为运行100个查询并不是一个大问题,前提是您未针对每个请求都运行该查询。

Are you expecting performance issues? 您是否期望性能问题? If that's the case, I'd probably use a cron job to run that query every X minutes.. 如果是这样,我可能会使用cron作业每X分钟运行一次该查询。

You could setup a view of the tables and do then run the delete sql against the view. 您可以设置表的视图,然后针对该视图运行delete sql。 That should delete the underlying table data as well. 那也应该删除基础表数据。 Your table schema and permissions could have an affect whether this will work or not. 您的表架构和权限可能会影响这一点是否起作用。 Check out this answer, it might help as well. 看看这个答案,可能也有帮助。

Does deleting row from view delete row from base table - MYsql? 从视图中删除行是否会从基表-MYsql中删除行?

This implies you create a new table for every account. 这意味着您为每个帐户创建一个新表。 Why are you not creating a record for each account within a single table? 为什么不在单个表中为每个帐户创建记录?

For example... 例如...

create table account (id int unsigned primary key auto_increment, other fields...);

If you alter your table structure you will be able to delete individual account records with a single query... 如果您更改表格结构,则可以通过单个查询删除单个帐户记录...

delete from account where condition=true;

Individual transaction records for each account are then stored in another table and contain the account id they relate to... 然后,每个帐户的个人交易记录存储在另一个表中,并包含它们与之相关的帐户ID。

create table transaction (id, account_id, other transaction fields);

If you don't change the database design you'll need to write PHP code that loops through each table and runs your delete query. 如果您不更改数据库设计,则需要编写遍历每个表并运行删除查询的PHP代码。 This is very inefficient and I urge you to redesign the table as suggested. 这是非常低效的,我敦促您按照建议重新设计表格。

If you don't understand why my table redsign suggestion is a better approach, post more information about your database and I'll explain in more detail with a working example. 如果您不明白为什么建议使用表重新命名是更好的方法,请发布有关您的数据库的更多信息,我将通过一个工作示例进行详细说明。

Please consider the following example. 请考虑以下示例。

I have three tables in following structure. 我有以下结构的三个表。

Table names : t1,t2,t3 表名:t1,t2,t3

Fields : Id, name 栏位:编号,名称

Im going to perform delete query with one condition which recode id must less than 10. 我将以一种条件执行删除查询,该条件的重新编码ID必须小于10。

DELETE FROM t1, t2,t3 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id<10 and t2.id<10 and t3.id<10.

The query has been successfully executed ( MySql ). 查询已成功执行(MySql)。 I got the expected output. 我得到了预期的输出。

So please try the same way with your condition. 因此,请根据您的情况尝试相同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM