Skip to content
Advertisement

MySQL- Create Duplicate Results in SELECT statement

I have a table like

ColumnA ColumnB ColumnC
ABC TextA 3
EFG TextB 1
EFG TextC 2

How do I write a SELECT statement that will return the # of rows for ColumnA and ColumnB based on the values of ColumnC

Expect results:

ColumnA ColumnB
ABC TextA
ABC TextA
ABC TextA
EFG TextB
EFG TextC
EFG TextC

Advertisement

Answer

You can use a recursive cte:

with recursive cte(a, b, c) as (
    select cola, colb, colc - 1 from t
    union all
    select a, b, c - 1 from cte where c > 0
)
select a, b from cte order by a;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement