SQL插入并根据其他表列将数据更新为列
我有两个桌子:
表产品表
| ProductID | ProductPrice | ProductSupplier |
+-----------+---------------+-----------------+
| 1 | 25 | CompanyA |
| 2 | 35 | CompanyB |
| 3 | 12 | CompanyC |
供应商供应
商供应商 | 名称 |
---|---|
1 | Companya |
2 | Companyb |
3 | Companyc |
如何根据表供应商的值却将新的列供应商插入表产品,但具有相应的column productsupplier值? 新的期望输出的示例: 餐桌产品
| ProductID | ProductPrice | ProductSupplier | SupplierID |
+-----------+---------------+-----------------+------------+
| 1 | 25 | CompanyA | ID value from table Suppliers |
| 2 | 35 | CompanyB | ID value from table Suppliers |
| 3 | 12 | CompanyC | ID value from table Suppliers |
I have two tables:
table PRODUCTS
| ProductID | ProductPrice | ProductSupplier |
+-----------+---------------+-----------------+
| 1 | 25 | CompanyA |
| 2 | 35 | CompanyB |
| 3 | 12 | CompanyC |
table SUPPLIERS
SupplierID | SupplierName |
---|---|
1 | CompanyA |
2 | CompanyB |
3 | CompanyC |
How to insert new column SupplierID into table Products, based on values from table Suppliers but with corresponding values from column ProductSupplier?
example of new desired output:
Table PRODUCTS
| ProductID | ProductPrice | ProductSupplier | SupplierID |
+-----------+---------------+-----------------+------------+
| 1 | 25 | CompanyA | ID value from table Suppliers |
| 2 | 35 | CompanyB | ID value from table Suppliers |
| 3 | 12 | CompanyC | ID value from table Suppliers |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用更新加入:
请注意,您正在朝着更正常化的方向移动,这是一件好事。假设您打算保留
供应商
表,则productsupplier
products 表中的表现在是多余的,并且可以删除:Use an update join:
Note that you are moving in the direction of more normalization, which is a good thing. Assuming you are intending to keep the
SUPPLIER
table, then theProductSupplier
column in thePRODUCTS
table is now redundant and can probably be dropped: