简体   繁体   English

SQL 使用选定记录列表中的随机数据填充表

[英]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.

相关问题 从记录列表中填充字典的 Pythonic 方法 - Pythonic way to populate a dictionary from list of records 从列表中随机选择的值的最近邻居? - Closest neighbor to value selected at random from list? 为从 Python 中的列表中选择的随机项目设置条件 - Setting a conditional for a random item selected from a list in Python 从函数列表中获取随机函数并调用所选函数 - Getting a random function from a list of functions and calling the selected function 如何将选定的要素从“随机森林”转换为新列表 - How to turn selected features from Random Forest into a new list 如何从SQL表下载大数据并通过一次获取1000个左右的记录连续保存到csv中 - How to download large data from a SQL table and consecutively save into csv by fetching 1000 or so records at once 如何创建从列表中选择的5个随机数组成的5个列表? - How do I create 5 lists of 5 random numbers selected from a list? 填充一个列表/列表列表,文件中没有重复数据 - populate a list / list of lists with no repeated data from a file 从 ascii 表中选择 5 个随机数据元素 - Selecting 5 random data elements from an ascii table Python - 从列表中提取随机数。 使用指定的长度和总和填充新列表 - Python - Pull random numbers from a list. Populate a new list with a specified length and sum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM