当我尝试连接两个表时,Mysql抛出“on clause子句中的未知列column_name”错误。

时间:2013-02-21 13:28:10

标签: mysql

我正试图通过两张桌子以下的方式展示价格低廉的酒店客房(酒店价格低廉,无需在该酒店内展示其他房间)。 但是我在on子句"中收到错误" Unknow column column_name。请检查我的查询

    SELECT  r.*,h.*
    FROM hotels_temp h,rooms_temp r
    INNER JOIN
    (
      SELECT 
        hotel_code,MIN(amount) mincost
      FROM 
        rooms_temp 
      GROUP BY hotel_code
    ) m ON r.amount = m.mincost and h.hotel_code=r.hotel_code 
    ORDER BY r.amount
    LIMIT 30

表名: hotels_temp

enter image description here

表名:rooms_temp

enter image description here

2 个答案:

答案 0 :(得分:1)

我可以通过以下查询自行解决这个问题。

    SELECT * FROM hotels_temp
    INNER JOIN
    (
        SELECT  r.amount,r.hotel_code FROM rooms_temp r
        INNER JOIN
        (
            SELECT hotel_code,amount, MIN(amount) mincost FROM rooms_temp  GROUP BY hotel_code
        ) m ON 
        r.amount = m.mincost GROUP BY r.hotel_code
    ) ht ON 
    ht.hotel_code=hotels_temp.hotel_code LIMIT 0,30

答案 1 :(得分:0)

根据μ假设,您需要的房间成本较低,每间酒店最多可容纳一个房间。然后你按照以下方式实现这个目标,

SELECT DISTINCT r.id, r.*,h.*
FROM rooms_temp r LEFT JOIN hotels_temp h ON h.hotel_code = r.hotel_code
ORDER BY r.amount DESC LIMIT 0,30

这里我假设r.id是房间的唯一身份。