MySQL create view joining two tables

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.

Tags:

Mysql

Join

View