简体   繁体   English

MySQL 创建连接两个表的视图

[英]MySQL create view joining two tables

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 table has information about users, items table has information about items and gifts table shows which user sent what gift to which user. users表包含有关用户的信息, items表包含有关项目的信息, gifts表显示哪个用户向哪个用户发送了什么礼物。

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

You can create a view with two tables like: 您可以使用两个表创建视图,例如:

 CREATE VIEW giftList AS
 SELECT users.user_from,users.user_to,gifts.gift_name,gifts.gift_price FROM users,gifts
 WHERE users.user_id = gifts.user_id;

The where clause is to make sure the output does not repeat. where子句用于确保输出不重复。

I believe were looking for data blending .我相信我们正在寻找数据混合 So basically having google data studio do a JOIN statement on ids from 2 data sets所以基本上让 google data studio 对来自 2 个数据集的 ids 做一个 JOIN 语句

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

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