Osvaldo Mercado
Osvaldo Mercado

Reputation: 971

Duplicate rows in MySQL select statement

I have the following query:

SELECT word, count
FROM t_word t
JOIN t_counter tc
ON t.word_id = tc.id

Which produces something like:

'cab', 2
'capital', 3
'new york', 2

What i want to do, is have the results show up like this:

'cab',
'cab'
'capital',
'capital',
'capital', 
'new york',
'new york'

So that the count column, actually duplicates that row for my query. I'm not entirely sure if this is possible, as I haven't played much with loops in MySQL... Any guidance is much appreciated!.

Upvotes: 0

Views: 361

Answers (1)

Thomas
Thomas

Reputation: 64635

Select T.word
From t_word As T
    Join    (
            Select 1 As Num
            Union All Select 2
            Union All Select 3
            ) As Numbers
        On Numbers.Num <= T.count

If count actually comes from the counter table, then you would do something like:

Select T.word
From t_word As T
    Join t_counter As TC
        On TC.id = t.word_id
    Join    (
            Select 1 As Num
            Union All Select 2
            Union All Select 3
            ) As Numbers
        On Numbers.Num <= TC.count

Upvotes: 1

Related Questions