[英]connecting records in 3 tables randomly
I have three tables: 我有三张桌子:
categories (id)
product_models(id, category_id)
products(id, product_id, category_id)
There are records on each table (lorem ipsum content), i want to connect the data. 每个表都有记录(lorem ipsum内容),我想连接数据。
I've changed already the product_models, executing this query: 我已经改变了product_models,执行了这个查询:
update product_models
set category_id = (select id from categories order by RAND() limit 1)
How can I use a single query that will map all the products to a product model and update the category also? 如何使用单个查询将所有产品映射到产品模型并更新类别?
您应该根据需要添加对表的约束和引用,以便在更新一个表时,其他表也将更新。
Assuming that your third table structure is (id, product_model_id, category_id), another assumption is that you have a composite foreign key (product_model_id, category_id), then you have to update the third table like below 假设您的第三个表结构是(id,product_model_id,category_id),另一个假设是您有一个复合外键(product_model_id,category_id),那么您必须更新第三个表,如下所示
UPDATE products P
INNER JOIN (SELECT id, category_id from product_models ) M
ON P.product_id = M.id
SET P.category_id = M.category_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.