mysql连接在同一个表上

时间:2014-01-18 21:36:59

标签: php mysql sql

node = id,title,kenya_hotel_id,tanzania_hotel_id

2,Fantastic package for kenya and Tanzania, 8, 5 
5,Delux package for kenya and tanzania, 8, 4 

hotel = id,name,star

4, simba continental, 3 star
5, african safari hotel, 4 star
8, kenya hotel, 5 star

我试过的Mysql语句如下

SELECT node.title AS title,
    hotel.name AS kenyan_hotel,
    hotel.star AS kenya_hotel_star,
    hotel.name AS Tanzanian_hotel,
    hotel.star AS Tanzanian_hotel_star
FROM
node
INNER JOIN node on node.kenya_hotel_id = hotel.id
LEFT JOIN node on node.tanzania_hotel_id = hotel.id

我想要结果如

title, kenya hotel name, kenya hotel star, tanzania hotel, tanzania hotel star

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您需要表别名

SELECT n.title AS title,
       hotelk.name AS kenyan_hotel,
       hotelk.star AS kenya_hotel_star,
       hotelt.name AS Tanzanian_hotel,
       hotelt.star AS Tanzanian_hotel_star
 FROM node n INNER JOIN
      hotel hotelk
      on n.kenya_hotel_id = hotelk.id LEFT JOIN
      hotel hotelt
      on n.tanzania_hotel_id = hotelt.id;