1
UPDATE item t 

INNER JOIN ( SELECT
               item_name,
               MAX( item_keyword ) AS item_keyword
             FROM item
             WHERE ca_id2  = '2010'
             GROUP BY item_name
           ) s ON t.item_name = s.item_name

SET t.item_keyword = s.item_keyword 

WHERE t.ca_id2 ='3010'

错误是:

1064 - 您的 SQL 语法有错误。查看与您的 MySQL 服务器版本相对应的手册,了解在 'SELECT item_name, max(item_keyword) AS item_keyword FROM item 附近使用的正确语法

MySQL 4.0.22 版本失败的原因是什么?

4

1 回答 1

1

我尝试根据 http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/构建您的查询

UPDATE item t

SET t.item_keyword =
      ( SELECT MAX( i.item_keyword ) AS item_keyword
          FROM ( SELECT item_keyword, item_name, cd_id2 FROM item ) AS i
         WHERE t.item_name = i.item_name
           AND i.cd_id2  = '2010'
      GROUP BY i.item_name
      )

WHERE t.ca_id2 ='3010'

或者你也可以试试这个版本

UPDATE item t

INNER JOIN ( SELECT item_keyword, item_name, cd_id2 FROM item ) AS i

SET t.item_keyword =
      ( SELECT MAX( i.item_keyword ) AS item_keyword
          FROM i
         WHERE t.item_name = i.item_name
           AND i.cd_id2  = '2010'
      GROUP BY i.item_name
      )

WHERE t.ca_id2 ='3010'

对不起,我没有 MySQL 4,所以你必须测试上面的查询。

其他读物:

如果上述查询不起作用,您可以尝试使用临时表而不是子选择:

http://sqlfiddle.com/#!2/13ccb/1

CREATE TEMPORARY TABLE s
   SELECT item_name,
          MAX( item_keyword ) AS item_keyword
    FROM  item
    WHERE cd_id2  = '2010'
      AND item.item_name IN ( SELECT item_name FROM item WHERE item.ca_id2='3010')
GROUP BY item_name;


UPDATE item t 
INNER JOIN s ON t.item_name = s.item_name
SET t.item_keyword = s.item_keyword 
WHERE t.ca_id2 ='3010';

DROP TEMPORARY TABLE s;
于 2012-07-01T12:04:24.113 回答