Create a view with column num_rows - MySQL

后端 未结 7 1409
[愿得一人]
[愿得一人] 2020-12-10 06:41

I need to create a view that has a column named row_num where it will be inserted the row number, just like an auto increment in a normal table.

Let\'s say I\'ve thi

相关标签:
7条回答
  • 2020-12-10 07:32

    Or try this-> create a temporary table and insert your data into it like bellow

    CREATE OR REPLACE TEMPORARY TABLE myview (
        country VARCHAR(250), 
        name VARCHA(50), 
        price VARCHAR(50), 
        row_num int(11)
    );
    
    SET @row_num = 0;
    INSERT INTO myview (country,name,price,row_num)
        SELECT @row_num:=@row_num+1 
            as country,name,price,row_num 
        FROM testing;
    
    SELECT * FROM myview;
    
    +---------+------+------+-------+---------+
    | country | name | age  | price | row_num |
    +---------+------+------+-------+---------+
    | Sweden  | Alex |   49 |    10 |       1 |
    | France  | Anne |   10 |    15 |       2 |
    | France  | Anne |   11 |    16 |       3 |
    | US      | john |   22 |    20 |       4 |
    +---------+------+------+-------+---------+
    
    0 讨论(0)
提交回复
热议问题