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

标签 mysql

我有两个表,供应商和订单。

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/

相关文章:

安装 mysql-live-select 后 MySQL Meteor 错误 :"bad formatting in HTML template"

php - 在我执行以下代码时使用 php、json 服务我得到一个空白页面,数据库中没有更新数据...如果错误请纠正我

mysql 合并 2 列 where 条件

MySql - 大型表 IP 配对的更好架构?

java - 为什么 DateTime 字段上的 getTimestamp 返回的结果与选择 unix_timestamp 的结果不同?

sql - 有没有一种干净的方法来清理 MySQL 中的重复条目?

mysql - 如何使用 chmod 作为 mysql 数据类型

php mysql INSERT INTO mysql 通过使用注册表单

mysql - MySQL Select 查询中子选择的效率

MySQL 删除 with where 不会报错