简体   繁体   English

在Mysql中生成Series,如何在查询时添加自动增量?

[英]Generate Series in Mysql, how to add a autoincrement at query?

I had a table with a column(c5) that contained a number x, I needed a SQL expression that repeated the same row x numbers of times: 我有一个带有包含数字x的column(c5)的表,我需要一个SQL表达式来重复同一行x次数:

My table A contained: 我的表A包含:

c1  c2  c3  c4  c5
16  1   2   16  3
16  1   2   17  2 
16  1   2   18  1

And I needed: 我需要:

c1  c2  c3  c4  c5  n
16  1   2   16  3   1
16  1   2   16  3   2
16  1   2   16  3   3
16  1   2   17  2   1
16  1   2   17  2   2
16  1   2   18  1   1

I solved That with SQL expression: 我用SQL表达式解决了:

SELECT
    c1, c2, c3, c4, c5, row_number AS n
FROM
    (
        SELECT
            @curRow := @curRow + 1 AS row_number
        FROM
            tablea
        JOIN (SELECT @curRow := 0) r
        WHERE
            @curRow < (
                SELECT
                    max(c5)
                FROM
                    tablea
            )
    ) AS vwtable2
LEFT JOIN tablea d ON vwtable2.row_number <= tablea.c5;

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

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