INSERT repeating values in SQL

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-10 22:22:40

问题


Trying to find a simple way to insert some repeating values in two columns in my table, something similar to the rep function in R-

for instance, I need to insert two values (chocolate and vanilla, 4 times each) and I need to insert 4 types of values that repeat twice such as --

flavor_type schedule_type
chocolate   weekly
chocolate   monthly
chocolate   quarterly
chocolate   yearly
vanilla     weekly
vanilla     monthly
vanilla     quarterly
vanilla     yearly

回答1:


You can use cross join:

select *
from (values('chocolate'), ('vanilla')) flavor(flavor_type)
cross join (values('weekly'), ('monthly'), ('quarterly'), ('yearly')) schedule(schedule_type)

Output:

flavor_type schedule_type
----------- -------------
chocolate   weekly
chocolate   monthly
chocolate   quarterly
chocolate   yearly
vanilla     weekly
vanilla     monthly
vanilla     quarterly
vanilla     yearly


来源:https://stackoverflow.com/questions/56926698/insert-repeating-values-in-sql

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