[英]SQL Populate table with random data from a selected list of records
Is there a SQL code that could populate my table in the following matter every 3seconds?是否有一个 SQL 代码可以每 3 秒在以下事项中填充我的表? I currently am struck for quite some time.
我目前震惊了很长一段时间。 Any kind souls?
有好心人吗?
I wish to populate the id in increasing manner, position from values 1-3, the action from the list of the given list (weightlifting,shoutout,dumbbells,facewipe,tornado, muscle) and the sync from values 0-3.我希望以递增的方式填充 id,值 1-3 的位置,给定列表列表中的动作(举重、大喊大叫、哑铃、面部擦拭、龙卷风、肌肉)以及值 0-3 的同步。
Just find your database's RANDOM function.只需找到您的数据库的 RANDOM 函数即可。 For sql-server:
对于 sql 服务器:
INSERT INTO your_table (position, action, sync)
SELECT
CAST(RAND() * 3 AS INT) + 1,
CASE CAST(RAND() * 8 AS INT)
WHEN 0 THEN 'Weighlifting'
WHEN 1 THEN 'Shoutout'
WHEN ...
END,
CAST(RAND() * 4 AS INT)
I'm assuming the id column is set to auto-increment and will take care of itself.我假设 id 列设置为自动递增并且会自行处理。
The RAND function, as most random number generator functions, will return a number in [0, 1).与大多数随机数生成器函数一样,RAND 函数将返回 [0, 1) 中的数字。 Multiply, add and floor the result accordingly, to get an integer in the desired range.
相应地将结果相乘、相加和地板,以获得所需范围内的整数。
I suggest you consider creating a table with the possible values for the "action" column and replacing the column with a FK to that table.我建议您考虑使用“action”列的可能值创建一个表,并将该列替换为该表的 FK。 It will make it simpler both to populate and maintain this table.
这将使填充和维护此表变得更简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.