简体   繁体   English

更新外键表的属性等于外键表的属性的列值

[英]Update column value where property of foreign key table is equal to property of foreign key table

I am looking to do an update basing myself on the property of another table to which my initial table has a foreign key to, so given the case I have 3 tables:我希望根据我的初始表具有外键的另一个表的属性来进行更新,因此考虑到我有 3 个表的情况:

Product
Id | Name | ProductCategoryId | GameId
1    Prd-A    1                   1
2    Prd-B    1                   1
3    Prd-C    3                   1

Game
Id | Name
1    Game A
2    Game B
3    Game C

ProductCategory
Id | Name
1    Category A
2    Category B
3    Category C

Is there a way for me to do something like有没有办法让我做类似的事情

UPDATE Product
SET GameId= (GameTable Where name = 'Game B').Id
WHERE ProductCategoryId= (ProductCategory Where name = 'Category C').Id;

The reason I cannot use the Id even knowing it is that I am dealing with different environments and the Id does not always match between environments, so I need to use another property that doesn't change.即使知道它我也不能使用 Id 的原因是我正在处理不同的环境并且 Id 并不总是在环境之间匹配,所以我需要使用另一个不会改变的属性。

https://www.db-fiddle.com/f/sNP8nZc8TsJNHGGqm8STYY/0 here is a fiddle with the example. https://www.db-fiddle.com/f/sNP8nZc8TsJNHGGqm8STYY/0这里是一个例子。

Do you need in this:你需要在这个:

UPDATE PRODUCT p
JOIN PRODUCTCATEGORY pc ON p.productcategoryid = pc.id
JOIN PRODUCTCATEGORY pcu ON pcu.name = 'CATEGORY B'
SET p.productcategoryid = pcu.id
WHERE pc.name = 'CATEGORY A';

https://www.db-fiddle.com/f/sNP8nZc8TsJNHGGqm8STYY/1 https://www.db-fiddle.com/f/sNP8nZc8TsJNHGGqm8STYY/1

UPDATE PRODUCT p 
JOIN PRODUCTCATEGORY pc ON pc.name = 'CATEGORY C' 
JOIN GAME g ON g.name = 'GAME B' 
SET p.gameid = g.id 
WHERE p.productcategoryid = pc.id;

This is what I was looking for given the example above, thanks Akina for the help鉴于上面的示例,这就是我一直在寻找的,感谢 Akina 的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM