Home > Back-end >  MySQL- Create Duplicate Results in SELECT statement
MySQL- Create Duplicate Results in SELECT statement

Time:10-13

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

CodePudding user response:

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;
  • Related