gpt4 book ai didi

mysql 最大严格模式

转载 作者:行者123 更新时间:2023-11-30 00:55:54 27 4
gpt4 key购买 nike

我有以下查询:

DELETE FROM table1 
WHERE node_id = ".$id."
AND date < (
SELECT
(MAX(date)- INTERVAL 1 MONTH)
from table1
WHERE node_id = ".$id."
)

但是由于启用了 mysql 严格模式,我收到错误:

ERROR 1093 (HY000): You can't specify target table 'table1' for update in FROM clause

经过在线调查后,我尝试重新创建查询,如下所示:

SELECT 
*
FROM table1 as tb1
INNER JOIN table1 as tb2
on tb1.id = tb2.id
HAVING tb1.date < MAX(tb2.date)-INTERVAL 1 MONTH AND tb1.node_id = 1;

但是结果集返回空。

我已将 max(tb2.date) 更改为手动输入的日期,这给了我预期的结果。

知道这里发生了什么吗?

最佳答案

你能试试这个吗?

使用 JOIN

数据

mysql> select * from test;
+---------+------------+
| node_id | dt |
+---------+------------+
| 1 | 2011-03-01 |
| 1 | 2011-02-01 |
| 1 | 2011-01-01 |
+---------+------------+
3 rows in set (0.00 sec)

选择

SELECT *
FROM (
SELECT MAX(dt) AS max_dt
FROM test
WHERE node_id = 1
) x INNER JOIN test ON test.node_id = 1 AND test.dt < x.max_dt;
+------------+---------+------------+
| max_dt | node_id | dt |
+------------+---------+------------+
| 2011-03-01 | 1 | 2011-01-01 |
| 2011-03-01 | 1 | 2011-02-01 |
+------------+---------+------------+

删除

DELETE test
FROM (
SELECT MAX(dt) AS max_dt
FROM test
WHERE node_id = 1
) x INNER JOIN test ON test.node_id = 1 AND test.dt < x.max_dt;
Query OK, 2 rows affected (0.02 sec)

检查

mysql> select * from test;
+---------+------------+
| node_id | dt |
+---------+------------+
| 1 | 2011-03-01 |
+---------+------------+
1 row in set (0.00 sec)

使用变量

在您的情况下,您试图仅删除一个node_id。这样就可以进行简单的查询。

SELECT @max_dt := MAX(dt)
FROM test
WHERE node_id = 1;
+--------------------+
| @max_dt := MAX(dt) |
+--------------------+
| 2011-03-01 |
+--------------------+


DELETE FROM test
WHERE node_id = 1
AND dt < @max_dt;
Query OK, 2 rows affected (0.00 sec)

顺便说一句

您提出了 17 个问题,但尚未接受任何答案。没有有用的答案吗?

关于mysql 最大严格模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20565999/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com