Repeat a result row multiple times by value in row

橙三吉。 提交于 2020-01-07 03:06:17

问题


i have a question about sql query. i want to duplicate a one row with same value, The number of lines must be same with the value of quantity

example i have 1 data like this

Item  ItemName   Quantity
B100  Mouse      10

but i want to be like this

Item  ItemName   Quantity
B100  Mouse      10
B100  Mouse      10
B100  Mouse      10
B100  Mouse      10
B100  Mouse      10
B100  Mouse      10
B100  Mouse      10
B100  Mouse      10
B100  Mouse      10
B100  Mouse      10

can you help me

Thanks Before


回答1:


Create a numbers table and just do a CROSS JOIN with it

select Item , ItemName ,  Quantity
from yourtable A
cross join (select num from numbers) b
where a.Quantity <= b.num

To generate sequential numbers in Mysql check the below answers

  • generate an integer sequence in MySQL
  • Generating a range of numbers in MySQL


来源:https://stackoverflow.com/questions/34253461/repeat-a-result-row-multiple-times-by-value-in-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!