你可以試試嗎?
使用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
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
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)
BTW
你問17個問題,但沒有接受任何的答案。沒有有用的答案嗎?
嘗試使用alice名稱表 – bgs
誰!!「£%是愛麗絲? – Strawberry