问题
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