2014-02-10 35 views
1

我不太清楚爲什麼,但我的代碼似乎無法工作。它或者我得到錯誤1052或1054. 這是我的代碼。請幫忙。MySQL錯誤1054:'on子句'中的未知列'hotels.postal_code'

SELECT 

    hotels.postal_code AS ' Hotel Postal Code', 
    name AS 'Hotel Name', 
    latitude AS 'Latitude', 
    longitude AS 'Longitude', 
    address AS 'Hotel Address', 
    hyperlink AS 'Hotel Hyperlink', 
    hotels.district AS 'Hotel District', 
    'hotel' AS type 

FROM 
    hotels 

     join 
    hotel_sales ON hotels.postal_code = hotel_sales.sales_id 
     join 
    postal_code_location ON hotels.district = postal_code_location.district 

UNION SELECT 

    malls.postal_code AS ' Mall Postal Code', 
    name AS 'Mall Name', 
    latitude AS 'Latitude', 
    longitude AS 'Longitude', 
    address AS 'Mall Address', 
    hyperlink AS 'Mall Hyperlink', 
    malls.district AS 'Mall District', 
    'mall' AS type 
FROM 
malls 

     join 
    hotel_sales ON hotels.postal_code = hotel_sales.sales_id 
     join 
    postal_code_location ON hotels.district = postal_code_location.district 
+1

你可以分享你的表中的DDL? – Mureinik

+0

'ON hotels.postal_code = hotel_sales.sales_id'似乎是錯誤的。聽起來對我來說應該是'ON hotels.hotel_id = hotel_sales.hotel_id'? –

+0

在您的第二個選擇中,您加入了'hotels.postal_code',但該查詢中沒有使用「酒店」。你正在從'商場'選擇。 –

回答

2

更換

FROM 
malls 

     join 
    hotel_sales ON hotels.postal_code = hotel_sales.sales_id 

AS

FROM 
malls 

     join 
    hotel_sales ON malls.postal_code = hotel_sales.sales_id 

加入MALLSHOTEL_SALES與右列...

所以最終的查詢是..

SELECT 

    hotels.postal_code AS ' Hotel Postal Code', 
    name AS 'Hotel Name', 
    latitude AS 'Latitude', 
    longitude AS 'Longitude', 
    address AS 'Hotel Address', 
    hyperlink AS 'Hotel Hyperlink', 
    hotels.district AS 'Hotel District', 
    'hotel' AS type 

FROM 
    hotels 

     join 
    hotel_sales ON hotels.postal_code = hotel_sales.sales_id 
     join 
    postal_code_location ON hotels.district = postal_code_location.district 

UNION SELECT 

    malls.postal_code AS ' Mall Postal Code', 
    name AS 'Mall Name', 
    latitude AS 'Latitude', 
    longitude AS 'Longitude', 
    address AS 'Mall Address', 
    hyperlink AS 'Mall Hyperlink', 
    malls.district AS 'Mall District', 
    'mall' AS type 
FROM 
malls 

     join 
    hotel_sales ON malls.postal_code = hotel_sales.sales_id 
     join 
    postal_code_location ON hotels.district = postal_code_location.district 
+0

你真的想編輯'hotels.district',然後我們再次得到「不行」:) – oerkelens

+0

好的問題解決了。 :)非常感謝大家! – iRandallT

1

此錯誤是在你的查詢有關

歧義COLUMN_ID

應該是這樣的

SELECT 
    hotels.postal_code AS ' Hotel Postal Code', 
    hotels.name AS 'Hotel Name', 
    hotels.latitude AS 'Latitude', 
.... 
相關問題