我有categories
带有列category_id
和的表name
。我想products
用随机数据生成新表。表products
有列product_id, category_id, name, price
。我想给每个产品随机category_id
。
我的表格生成脚本products
:
INSERT INTO products (category_id, name, price)
SELECT
(SELECT category_id FROM categories OFFSET floor(random()*50) LIMIT 1),
('Product ' || i),
round(cast(random() * 999 + 1 as numeric), 2)
FROM generate_series(1,100) as seq(i)
此脚本生成 100 行产品,但每个产品都有相同的category_id
. 我做错了什么?