仅在一个转换倍数MySQL DELETE

时间:2015-01-08 12:47:26

标签: mysqli row sql-delete

我有以下疑问:

DELETE FROM server_N1 where vencimiento <= '2015/01/08';
DELETE FROM server_N2 where vencimiento <= '2015/01/08';
DELETE FROM server_N3 where vencimiento <= '2015/01/08';
...
DELETE FROM server_N11 where vencimiento <= '2015/01/08';

我想只在一个查询中转换这些查询。我尝试了以下但是它不起作用:

DELETE
N1, N2, N3, N4, N5, N6, N7, N8, N9, N10, N11 FROM server_N1 N1, server_N2 N2, server_N3 N3, server_N4 N4, server_N5 N5, server_N6 N6, server_N7 N7, server_N8 N8, server_N9 N9, server_N10 N10, server_N11 N11
WHERE
(N1.vencimiento <= '2015/01/08') AND
(N2.vencimiento <= '2015/01/08') AND
(N3.vencimiento <= '2015/01/08') AND
(N4.vencimiento <= '2015/01/08') AND
(N5.vencimiento <= '2015/01/08') AND
(N6.vencimiento <= '2015/01/08') AND
(N7.vencimiento <= '2015/01/08') AND
(N8.vencimiento <= '2015/01/08') AND
(N9.vencimiento <= '2015/01/08') AND
(N10.vencimiento <= '2015/01/08') AND
(N11.vencimiento <= '2015/01/08');

我该怎么办?

编辑:问题是当我尝试仅加入一个查询时,我收到以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE FROM server_N2 WHERE vencimiento <= '2015/01/08'; DELETE FROM server_N3 W' at line 1

编辑:代码:

$fechaactual = '2015/01/08';
$delete = mysqli_query($con, "DELETE FROM server_N1 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N2 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N3 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N4 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N5 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N6 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N7 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N8 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N9 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N10 WHERE vencimiento <= '{$fechaactual}'; DELETE FROM server_N11 WHERE vencimiento <= '{$fechaactual}'");

2 个答案:

答案 0 :(得分:1)

问题是一个或多个表没有要删除的行。由于没有符合其中一个条件的行,因此结果集为空 - 因此不会删除任何内容。

如果您知道其中一个表的行符合条件,那么您可以从该表开始,并使用left join子句中的条件on。但是,如果您不知道哪个表包含此类行,则查询会更难。

最后,更简单的在事务中包装delete语句可能会获得更好的性能,因此它们都会同时生效。

当然,这就引出了为什么你有11个具有相同结构的表的问题。这通常表明数据库设计不佳。这些表中的记录可能都在一个表中,其中一列定义了&#34; N&#34;值。那么你甚至不需要问这个问题 - 一个简单的删除就足够了。

编辑:

试试这个:

DELETE s FROM server_N1 s where vencimiento <= '2015/01/08';

答案 1 :(得分:0)

我认为你应该使用游标 ...

以下是有关其工作原理的教程: http://www.mysqltutorial.org/mysql-cursor/