彻底研究以下内容……看不到子查询……
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(rownum INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,my_column INT NOT NULL
,next_retry_time DATETIME NOT NULL
,destaddr VARCHAR(20) NOT NULL
);
INSERT INTO my_table (my_column,next_retry_time,destaddr)
VALUES
(101,NOW(),'my_string'),
(102,NOW(),'not_my_string'),
(101,NOW(),'not_my_string'),
(102,NOW(),'not_my_string'),
(102,NOW(),'my_string'),
(103,NOW(),'my_string');
SELECT * FROM my_table;
+--------+-----------+---------------------+---------------+
| rownum | my_column | next_retry_time | destaddr |
+--------+-----------+---------------------+---------------+
| 1 | 101 | 2013-04-04 12:50:51 | my_string |
| 2 | 102 | 2013-04-04 12:50:51 | not_my_string |
| 3 | 101 | 2013-04-04 12:50:51 | not_my_string |
| 4 | 102 | 2013-04-04 12:50:51 | not_my_string |
| 5 | 102 | 2013-04-04 12:50:51 | my_string |
| 6 | 103 | 2013-04-04 12:50:51 | my_string |
+--------+-----------+---------------------+---------------+
SELECT *
FROM my_table x
JOIN my_table y
ON y.my_column = x.my_column
WHERE y.destaddr = 'my_string'
AND y.rownum < 3;
+--------+-----------+---------------------+---------------+--------+-----------+---------------------+-----------+
| rownum | my_column | next_retry_time | destaddr | rownum | my_column | next_retry_time | destaddr |
+--------+-----------+---------------------+---------------+--------+-----------+---------------------+-----------+
| 1 | 101 | 2013-04-04 12:50:51 | my_string | 1 | 101 | 2013-04-04 12:50:51 | my_string |
| 3 | 101 | 2013-04-04 12:50:51 | not_my_string | 1 | 101 | 2013-04-04 12:50:51 | my_string |
+--------+-----------+---------------------+---------------+--------+-----------+---------------------+-----------+
UPDATE my_table x
JOIN my_table y
ON y.my_column = x.my_column
SET x.next_retry_time = NOW()
WHERE y.destaddr = 'my_string'
AND y.rownum < 3;
SELECT * FROM my_table;
+--------+-----------+---------------------+---------------+
| rownum | my_column | next_retry_time | destaddr |
+--------+-----------+---------------------+---------------+
| 1 | 101 | 2013-04-04 12:52:28 | my_string |
| 2 | 102 | 2013-04-04 12:50:51 | not_my_string |
| 3 | 101 | 2013-04-04 12:52:28 | not_my_string |
| 4 | 102 | 2013-04-04 12:50:51 | not_my_string |
| 5 | 102 | 2013-04-04 12:50:51 | my_string |
| 6 | 103 | 2013-04-04 12:50:51 | my_string |
+--------+-----------+---------------------+---------------+