Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的表格配置文件中有两列,它们是id和education。现在我想随机分配可以在这个集合中的教育字段值('HA','BA,'CA' and 'DA')。我怎样才能在一个命令中做到这一点。id是该表的主键。
id
education
('HA','BA,'CA' and 'DA')
如下所述:ELT(N,str1,str2,str3,…)
ELT(N,str1,str2,str3,…)
返回str1if N = 1、str2ifN = 2等。
str1
N = 1
str2
N = 2
如下所述RAND():
RAND()
要获得R范围内的随机整数i <= R < j,请使用表达式FLOOR(i + RAND() * (j – i))。
R
i <= R < j
FLOOR(i + RAND() * (j – i))
所以:
UPDATE my_table SET education = ELT(FLOOR(1 + RAND() * 4), 'HA', 'BA', 'CA', 'DA')
在sqlfiddle上查看。