我正在输入以下查询,但收到表格不可更新的错误消息?
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)
我想要实现的是通过添加一个序列列来加速我的查询,我可以对结果进行排序,而不是在多列上进行排序。它基于一个涉及5列的ORDER BY子句,其中一列来自JOINed表。
答案 0 :(得分: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 ....