我有两个表,供应商和订单。
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
想知道我是否可以做一个用它的名字(在供应商表上引用)替换 id_supplier 的选择语句,所以我最终会得到这样的东西:
name_supplier | date
--------------------------
lexmark | 2016-01-01
lexmark | 2016-01-05
dell | 2016-01-06
想知道这是否是正确的方法,或者我是否应该简单地将它的名称插入到订单表中。提前致谢!
最佳答案
一个简单的连接就是你所需要的
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
关于mysql用另一个表中引用的名称替换id的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40764327/