1

I'm inputting the following query but getting an error message that the table is not updateble?

SET @rownumber = 0;
UPDATE (
SELECT _atc_codes.se, diagnoses.*
FROM diagnoses
JOIN _atc_codes
ON (_atc_codes.id = diagnoses.atc_code)
ORDER BY _atc_codes.se, diagnoses.year, diagnoses.county, diagnoses.age_group, diagnoses.gender) AS b
SET b.base_order_index = (@rownumber:=@rownumber+1)

What I'm trying to achieve is to speed up my queries by adding a sequence column that I can sort my results on instead of sorting on multiple columns. It's based on an ORDER BY clause involving 5 columns, one of the from a JOINed table.

4

1 回答 1

0

如果您正在寻找自动增量,请按照以下示例进行操作。如果您不关心速度,只需将表替换为您的子查询

SELECT mycols, @n := @n + 1 AS rownum
   FROM MYTABLE , (SELECT @n := 0) as rownumstart

获得您的订单:

step1 添加一个 rownum 加入

step2 通过以正确的顺序将它与我们的同一张表的子查询连接来更新表,将 table.rownum 与 subquery.new_rownum 连接起来

ALTER MYTABLE ADD rownum int (11) unsigned NULL DEFAULT NULL;

UPDATE MYTABLE ,

  (SELECT @n := 0) AS ALIAS
SET rownum=@n := @n + 1 ;


UPDATE TABLE
INNER JOIN
  (SELECT ordered.*, @n := @n + 1 AS rownum
   FROM
     (SELECT WITH your
      ORDER WITHOUT rid
      FROM TABLE) AS ordered,

     (SELECT @n := 0) AS ALIAS) ordered_with_rownum ON ordered_with_rownum.rownum=TABLE.rownum
SET TABLE.columnA=ordered_with_rownum.columnA,
          TABLE.columnB=ordered_with_rownum.columnB ....
于 2013-09-25T10:43:51.757 回答