显示所有已预订房间的客户

时间:2018-08-06 03:00:25

标签: php sql subquery

子查询返回多于1行,这是错误。我要打印所有显示booked roomclient id的{​​{1}}详细信息。

owner id

1 个答案:

答案 0 :(得分:2)

子查询中的联接太多:

SELECT r.room_id, r.roomsize, r.numberroom, r.price, u.username,
       (SELECT u.username
        FROM user_info u 
        WHERE r.owner_id = u.user_id  
       ) as owner_name 
FROM room_register r INNER JOIN
     booked b 
     ON b.room_id = r.room_id INNER JOIN
     user_info u 
     ON u.user_id = b.booked_by;

或仅使用两个单独的JOIN

SELECT r.room_id, r.roomsize, r.numberroom, r.price, u.username,
       uo.username as owner_name 
FROM room_register r INNER JOIN
     booked b 
     ON b.room_id = r.room_id INNER JOIN
     user_info u 
     ON u.user_id = b.booked_by LEFT JOIN
     user_info uo
     ON uo.user_id = r.owner_id