-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 に答える