-2

我知道我必须为子查询添加别名,但我不确定在哪里添加它

询问:

UPDATE RETRY_MESSAGE_%d SET NEXT_RETRY_TIME = NOW() 
   WHERE NEW_SCMSGREF = 
     (SELECT NEW_SCMSGREF FROM 
     (SELECT NEW_SCMSGREF FROM RETRY_MESSAGE_%d 
         where DESTADDR = :destAddr ORDER BY PRIORITY, SCTS) 
     WHERE rownum < 2) AS TEMP

我可以尝试什么来解决这个问题?

4

1 回答 1

1

彻底研究以下内容……看不到子查询……

 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     |
 +--------+-----------+---------------------+---------------+
于 2013-04-04T11:54:48.807 回答