Problem Using ROW_NUMBER() function in MariaDB

前端 未结 1 1416
轻奢々
轻奢々 2021-01-23 20:11

I would like to have a row number column in a select table output, but when I try using the ROW_NUMBER() function MariaDB throws a syntax error. There are several references on

相关标签:
1条回答
  • 2021-01-23 20:31

    Window functions are supported in MariaDB 10.2 or higher version only.

    MariaDB 10.2 or higher:

    SELECT 
        MyData.*,
        ROW_NUMBER() OVER ( ORDER BY ID ) as Therow
    FROM MyData 
    WHERE Date_Reading > Now()- INTERVAL 3 HOUR;
    

    For lower version:

    We can use the MySQL variable to do this job.

    SELECT 
        MyData.*, 
        @row_num:= @row_num + 1 AS Therow
    FROM 
        MyData, 
        (SELECT @row_num:= 0 AS num) AS c
    WHERE Date_Reading > Now()- INTERVAL 3 HOUR
    ORDER BY test.`date` ASC;
    
    0 讨论(0)
提交回复
热议问题