我有3张桌子
我需要检查cat.category == product.category和subcat.sub_category == product.sub_category然后将cat和subcat中的id插入到产品category_id,sub_category_id
SELECT p.*, c.category AS category_name, s.sub_category AS subcategory_name
FROM product AS p
LEFT JOIN subcat AS s ON p.sub_category = s.sub_category
LEFT JOIN cat AS c ON p.category = c.category
答案 0 :(得分:3)
期待您的架构
您似乎正在根据类别和子类别的值查找表产品category_id,sub_category_id列的更新
您可以使用带连接的更新
update product p
inner join subcat AS s ON p.sub_category = s.sub_category
inner join cat AS c ON p.category = c.category
set p.category_id = c.id,
p.sub_category_id = s.id