2011-04-25 25 views
0

我有以下查詢:重複行

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' 

因此,計數列實際上爲我的查詢重複該行。我不完全確定這是否可能,因爲我在MySQL中沒有玩過太多循環......任何指導都非常感謝!

+0

是否'count'來自'counter'表? – Thomas 2011-04-25 20:10:33

+0

在SQL中真的值得這麼做嗎?看起來在代碼中處理數據並相應地複製數據會容易得多。 – Compeek 2011-04-25 20:29:20

回答

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 
+0

@Thomas:Union All語句似乎打破了查詢...得到一個錯誤的語法錯誤,是上面的查詢可用於MySQL 5.5 ?.是的,「櫃檯」確實來自櫃檯。 – 2011-04-25 20:21:06

+0

@ user550178 - 這是我的壞處。我修正了派生表中的拼寫錯誤。應該是'Union All Select ..'。 – Thomas 2011-04-25 20:27:51

+0

@ user550178 - 修復了第二個On子句中的另一個輸入錯誤,它應該是:'On Numbers.Num <= TC.count' – Thomas 2011-04-25 20:30:20