MySQL select语句中的重复行

时间:2011-04-25 20:04:13

标签: mysql duplicates

我有以下查询:

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

产生类似的东西:

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

我想做的是将结果显示如下:

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

因此count列实际上会为我的查询复制该行。我不完全确定这是否可行,因为我在MySQL中没有玩过很多循环...任何指导都非常感谢!

1 个答案:

答案 0 :(得分:1)

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

如果count实际上来自counter表,那么您可以执行以下操作:

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