使用MySQL加入4个表

时间:2017-05-24 06:17:28

标签: mysql join

我想加入的表:

    order:
    order_id | id_user | movie_title | date | time |

    seat_order:
    seat_order_id | order_id | id_seat |

    seat:
    seat_id | row | number | status |

    user:
    user_id | username | email | password |

我希望加入后的结果如下:

| email | movie_title | date | time | seat | status |

我可以使用此查询加入三个表,但是当我尝试加入user表时,它总是显示错误。

SELECT order.movie_title,date,time,user_id,
       seat_order.seat_id,
       seat.row,number,status
FROM order
JOIN seat_order
ON order.order_id=seat_order.order_id
JOIN seat
ON seat_order.seat_id=seat.seat_id
WHERE seat.status='reserved'

我需要您的帮助才能毫无错误地加入user表。

2 个答案:

答案 0 :(得分:0)

您可以做的最简单的连接是:

SELECT user.email, order.movie_title, order.date, order.time, seat.row, seat.number, seat.status
FROM
user
INNER JOIN order ON user.user_id = order.id_user
INNER JOIN seat_order ON order.order_id = seat_order.order_id
INNER JOIN seat ON seat_order.seat_order_id = seat.seat_id

答案 1 :(得分:-1)

请查看以下查询,

SELECT 
user.email, 
order.movie_title, 
order.date, 
order.time, 
seat.row, 
seat.number, 
seat.status 
FROM 
user 
INNER JOIN order on order.id_user = user.user_id 
INNER JOIN seat_order on seat_order.order_id = order.order_id 
INNER JOIN seat on seat.seat_id = seat_order.id_seat;