MySQL: 使用现有 table 中的随机函数生成派生列
MySQL: using random function in existing table for generating derived column
我有一个 table 'product' 列 'category',它由 10 个不同的类别组成,没有列 category_id。我想为每个类别派生一个带有新列 category_id 的 table。
select product_id,description,category, round(rand(10)) as category_id from product;
select product_id,description,category, rand( over partition by 10) from product;
使用上面的查询我已经尝试了 "round(rand())" 但是这只给出了 0 和 1,但我希望它为从 1 到 10 的 10 个产品分配 category_id。
我还想从两个随机类别中检索产品
使用 FLOOR(1+ rand() * 10) 这会给你一个 1 到 10 之间的数字
select product_id,description,category,FLOOR(1+ rand() * 10) as category_id from product;
我有一个 table 'product' 列 'category',它由 10 个不同的类别组成,没有列 category_id。我想为每个类别派生一个带有新列 category_id 的 table。
select product_id,description,category, round(rand(10)) as category_id from product;
select product_id,description,category, rand( over partition by 10) from product;
使用上面的查询我已经尝试了 "round(rand())" 但是这只给出了 0 和 1,但我希望它为从 1 到 10 的 10 个产品分配 category_id。
我还想从两个随机类别中检索产品
使用 FLOOR(1+ rand() * 10) 这会给你一个 1 到 10 之间的数字
select product_id,description,category,FLOOR(1+ rand() * 10) as category_id from product;