简体   繁体   English

有没有办法按组更新带有行号的 MySQL v5.5 表?

[英]Is there a way to UPDATE a MySQL v5.5 table with row numbers by group?

I only have version 5.5 of MySQL so the ROW_NUMBER() function does not exist.我只有 MySQL 的 5.5 版,所以 ROW_NUMBER() function 不存在。 Using this as a test table:将其用作测试表:

CREATE TABLE test(recordID INT(3), row_number INT(6), netID INT(6), 
              var1 VARCHAR(5), var2 VARCHAR(5)) ;

INSERT INTO test VALUES
(1, 1, 2388, "Bill",  "Smith"),
(2, 1, 2388, "Tom",   "Smith"),
(3, 1, 2388, "Pat",   "Smith"),
(4, 1, 2390, "Fred",  "Smith"),
(5, 1, 2390, "John",  "Smith"),
(6, 1, 2390, "Hal",   "Jone"),
(7, 1, 2399, "Deb",   "Jones"),
(8, 1, 2399, "Keith", "Mack");

I've written the following SELECT, it returns row_number as I need it but I have had no luck converting it to an UPDATE.我写了以下 SELECT,它返回我需要的 row_number 但我没有运气将它转换为 UPDATE。

SELECT 
    @row_number:=CASE
        WHEN @netID_no = netID 
          THEN @row_number + 1
          ELSE 1
        END AS row_number, recordID,
    @netID_no:= netID netID, var1, var2
FROM
    test,
    (SELECT @netID_no:=0,@row_number:=0) as t
   WHERE netID <> 0
ORDER BY 
    netID ASC, recordID;

I tried to make the select a MySQL variable like this;我试图像这样使 select 成为 MySQL 变量;

SET @mycode:=( SELECT … etc );
UPDATE NetLog SET row_number = (@mycode) WHERE netID = @netID_no;

But I get all sorts of ambiguous syntax errors.但是我遇到了各种模棱两可的语法错误。 I'm not sure how to proceed from here, would some please give me an example?我不确定如何从这里开始,有人可以给我举个例子吗?

You can't interpolate subqueries with a user-defined variable.您不能使用用户定义的变量插入子查询。 Variables take the place of a scalar value only, not expressions or subqueries or identifiers or anything else.变量只代替标量值,而不是表达式、子查询、标识符或其他任何东西。 Only use a variable where you would otherwise use a single string or numeric literal.仅在您将使用单个字符串或数字文字的地方使用变量。

Here's a solution:这是一个解决方案:

SET @netid=0, @row_number=0;

UPDATE test
SET row_number = CASE netID
    WHEN @netID THEN @row_number:=@row_number+1
    ELSE ((@netID:=netID)-@netID) + (@row_number:=1)
    END
ORDER BY netID, recordID;

Result:结果:

+----------+------------+-------+-------+-------+
| recordID | row_number | netID | var1  | var2  |
+----------+------------+-------+-------+-------+
|        1 |          1 |  2388 | Bill  | Smith |
|        2 |          2 |  2388 | Tom   | Smith |
|        3 |          3 |  2388 | Pat   | Smith |
|        4 |          1 |  2390 | Fred  | Smith |
|        5 |          2 |  2390 | John  | Smith |
|        6 |          3 |  2390 | Hal   | Jone  |
|        7 |          1 |  2399 | Deb   | Jones |
|        8 |          2 |  2399 | Keith | Mack  |
+----------+------------+-------+-------+-------+

I'm using a trick in ((.netID:.netID)-.netID) because that assigns the .netID with the value in the current row's netID column.我在((.netID:.netID)-.netID)中使用了一个技巧,因为它将 .netID 分配给当前行的.netID列中的netID Then it subtracts that same value, thus the expression yields zero.然后它减去相同的值,因此表达式产生零。

This is the kind of awkward trick you have to resort to.这是你不得不诉诸的尴尬把戏。 You'll be happier when you can upgrade to MySQL 8.0 and use window functions.当您可以升级到MySQL 8.0并使用window功能时,您会更开心。

Eg:例如:

UPDATE test a 
  JOIN
     ( SELECT x.recordid
            , COUNT(*) n 
         FROM test x 
         JOIN test y 
           ON y.netid = x.netid 
          AND y.recordid <= x.recordid 
        GROUP 
           BY x.recordid
      ) b
     ON b.recordid = a.recordid
    SET a.row_number = b.n;
    
SELECT * FROM test;
+----------+------------+-------+-------+-------+
| recordID | row_number | netID | var1  | var2  |
+----------+------------+-------+-------+-------+
|        1 |          1 |  2388 | Bill  | Smith |
|        2 |          2 |  2388 | Tom   | Smith |
|        3 |          3 |  2388 | Pat   | Smith |
|        4 |          1 |  2390 | Fred  | Smith |
|        5 |          2 |  2390 | John  | Smith |
|        6 |          3 |  2390 | Hal   | Jone  |
|        7 |          1 |  2399 | Deb   | Jones |
|        8 |          2 |  2399 | Keith | Mack  |
+----------+------------+-------+-------+-------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM