我有 2 张桌子
表名:Attributes
attribute_id | attribute_name
1 attr_name_1
2 attr_name_2
3 attr_name_1
4 attr_name_2
表名:Products
product_id | product_name | attribute_id
1 prod_name_1 1
2 prod_name_2 2
3 prod_name_3 3
4 prod_name_4 4
如果可以看到,attribute_id表Products中有以下 id 的(1,2,3,4),而不是(1,2,1,2).
问题出在表中Attributes,即存在attribute_names具有不同 ID 的重复值(),所以我想要:
- 从表中选择重复的一个 ID
Attributes Products使用“挑选”的 ID更新表(仅在attribute_id表中具有相同名称的情况下Attributes)- 之后,从表中删除重复值,在表
Attributes中没有用处Products
输出:
表名:Attributes
attribute_id | attribute_name
1 attr_name_1
2 attr_name_2
表名:Products
product_id | product_name | attribute_id
1 prod_name_1 1
2 prod_name_2 2
3 prod_name_3 1
4 prod_name_4 2
SQLFiddle上的演示
笔记:
如果我使用 sql 而不是手动修复此问题,它将对我有很大帮助。