【问题标题】:i want to insert data randomly in PostgreSQL 500 K row?我想在 PostgreSQL 500 K 行中随机插入数据?
【发布时间】:2020-11-04 18:03:52
【问题描述】:
INSERT INTO interactions(decription,drugcode,diseasecode,type)
VALUES ('xxas',224,56366,2);

我的 id 自动递增 类型应该是 1 或 2 而已

我的桌子

  • id int 自增主键
  • 说明 varchar(100) 不为空
  • 药品代码 int 不为空
  • 疾病代码 int 不为空
  • 类型 int 不为空

类型的值 (1 或 2 ) 只是 如何随机插入 500 k 行

【问题讨论】:

  • 类型应该是1或2而已
  • 你能解释清楚一点吗?
  • 我想随机插入数据,比如 500 k 行 id int auti incremant deription varchar(100) drug code int diseasecode int type int 把 type 的值设为 (1,2)
  • edit你的问题并解释你卡在哪里,出现什么错误等等......

标签: sql postgresql random sql-insert


【解决方案1】:

您可以为此使用 generate_series()。

INSERT INTO interactions (decription, drugcode, diseasecode, type)
select md5(random()::text),  -- some random text
       (random() * 999 + 1)::int, -- a random number between 1 and 1000
       (random() * 49 + 1)::int, -- a random number between 1 and 50
       (random() + 1)::int -- a random number between 1 and 2
from generate_series(1,500000);

【讨论】:

  • @ahmadnoor:,前面有错from
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 2018-01-14
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多