简体   繁体   English

mysql用另一个表中引用的名称替换id的列

[英]mysql replacing column of id's with names referenced in another table

i have two tables, suppliers and orders. 我有两张桌子,供应商和订单。

suppliers table

id  |  name
-------------
1   |  dell
2   |  lexmark
3   |  xerox


orders table

id_supplier  |  date
--------------------------
2            |  2016-01-01
2            |  2016-01-05
1            |  2016-01-06

Was wondering if i can make a select statement that substitutes the id_supplier by it's name (referenced on the suppliers table) so i'd end up with something like this: 想知道我是否可以制作一个select语句,用它的名字替换id_supplier(在供应商表上引用),所以我最终会得到这样的结果:

name_supplier  |  date
--------------------------
lexmark        |  2016-01-01
lexmark        |  2016-01-05
dell           |  2016-01-06

Would like to know if this is the correct aproach or if i should simply insert it's name in the orders table. 想知道这是不是正确的方法,或者我应该简单地在订单表中插入它的名字。 Thanks in advance! 提前致谢!

A simple join is what you need 简单的连接就是您所需要的

SELECT name as name_supplier, `date` FROM suppliers INNER JOIN orders on suppliers.id = orders.id_supplier

Would like to know if this is the correct aproach Yes definitely, it's the correct approach 想知道这是否是正确的方法是肯定的,这是正确的方法

or if i should simply insert it's name in the orders table. 或者如果我只是在订单表中插入它的名字。 nopes, you should not have repeating data like that. 不,你不应该有这样的重复数据。 Please refer: https://en.wikipedia.org/wiki/Database_normalization 请参阅: https//en.wikipedia.org/wiki/Database_normalization

您可以使用join来执行此操作:

Select sp.name as name_supplier, ors.date from suppliers sp join orders ors on ors=sp.id

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

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