MySQL create view joining two tables(MySQL 创建视图连接两个表)
问题描述
如何创建将不同列与不同表合并的视图?例如我有三个表:用户、物品和礼物(在这个例子中是一个用户可以向另一个用户赠送礼物的系统)
How can I create a view that merges different columns with a different table? I have three tables for example: users, items and gifts (in this example it's a system that a user can give a gift to another user)
users
表包含有关用户的信息,items
表包含有关项目的信息,gifts
表显示哪个用户向哪个用户发送了什么礼物.
users
table has information about users, items
table has information about items and gifts
table shows which user sent what gift to which user.
我想要的是创建一个如下所示的视图:
What I want is to create a view like following:
user_from | user_to | gift_name | gift_price
sally | john | Teddy Bear | 10
推荐答案
必须先连接三个表.示例
You must join the three tables first. Example
CREATE VIEW GiftsList
AS
SELECT b.name user_from,
c.name user_to,
d.name gift_name,
d.price gift_price
FROM gift a
INNER JOIN users b
ON a.user_from = b.id
INNER JOIN users c
ON a.user_from = c.id
INNER JOIN items d
ON a.item = d.id
这篇关于MySQL 创建视图连接两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 创建视图连接两个表


- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01