2011-08-24 38 views
1

我一直在解决使用Join-Ons而不是逗号连接导致的问题。我的SQL目前看起来是这样的:在使用连接时未知列

SELECT islandID AS parentIslandID, islandName, island.longDesc, 
imageLocation, COUNT(resort.resortID) AS totalResorts, resort.resortID 
FROM island, resort, images 
join resort as r1 
on island.islandID = resort.parentIslandID 
where 
r1.resortID IN ( 
59,62,65,69,71,72,74,75,76,82,86,89,91,93,95,105, 
106,116,117,118,120,121,122,123,124,125,126,127, 
131,145,146,150,157,159,160,167,170,174,176,185,188,189,193, 
194,198,199,200,203,205,213,217 
) 
&& resort.active = '-1' 
GROUP BY resort.parentIslandID 
ORDER BY totalResorts DESC 

在执行时,我得到以下错误:

#1054 - Unknown column 'island.islandID' in 'on clause'

我做了一些研究和了解,但是我已经尽力纠正错误的由来通过为“岛”表创建一个别名来解决这个问题。当我这样做时,像“island.longDesc”这样的列就是“未知的”。

如果任何人都可以纠正似乎是一个小问题的语法问题,我将不胜感激。我更习惯于使用逗号连接,因此与我无关。感谢您的任何帮助 -

-Aaron

编辑:

Images Structure: 
CREATE TABLE `images` (
    `imageID` int(11) NOT NULL auto_increment, 
    `imageType` int(11) NOT NULL COMMENT 'used to tell if its for an artist, header image, etc.', 
    `parentObjectID` int(11) NOT NULL COMMENT 'used to tell what island/resort the image applies to', 
    `imageLocation` text NOT NULL, 
    `largeImageLocation` text NOT NULL, 
    `imageLinkLabel` text NOT NULL, 
    `imageURL` text NOT NULL, 
    PRIMARY KEY (`imageID`) 
) 

Island Structure: 
CREATE TABLE `island` (
    `islandID` int(11) NOT NULL auto_increment, 
    `islandName` text NOT NULL, 
    `shortDesc` text NOT NULL, 
    `longDesc` text NOT NULL, 
    `getTo` text NOT NULL, 
    `getAround` text NOT NULL, 
    `photoInfo` text NOT NULL, 
    `flowerInfo` text NOT NULL, 
    `musicInfo` text NOT NULL, 
    `cakeInfo` text NOT NULL, 
    `activityInfo` text NOT NULL, 
    `wedCoord` text NOT NULL, 
    `regs` text NOT NULL, 
    `climate` text NOT NULL, 
    `languageID` int(11) NOT NULL, 
    `currencyID` int(11) NOT NULL, 
    `wideAccept` int(11) NOT NULL, 
    `passportReq` int(11) NOT NULL, 
    `picture` text NOT NULL, 
    `daysSearchable` int(11) NOT NULL, 
    `active` tinyint(1) NOT NULL, 
    PRIMARY KEY (`islandID`) 
) 

Resort Structure: 
CREATE TABLE `resort` (
    `resortID` int(11) NOT NULL auto_increment, 
    `resortName` text NOT NULL, 
    `parentIslandID` int(11) NOT NULL, 
    `longDesc` text NOT NULL, 
    `website` text NOT NULL, 
    `genBooking` text NOT NULL, 
    `eventCoord` text NOT NULL, 
    `amenInfo` text NOT NULL, 
    `roomInfo` text NOT NULL, 
    `coordInfo` text NOT NULL, 
    `localeInfo` text NOT NULL, 
    `spaInfo` text NOT NULL, 
    `roomPrice` text NOT NULL, 
    `maxGuests` text NOT NULL, 
    `picture` text NOT NULL, 
    `search_Inclusive` int(11) NOT NULL, 
    `search_resortType` int(11) NOT NULL, 
    `search_onBeach` int(11) NOT NULL, 
    `search_wedCoord` int(11) NOT NULL, 
    `search_roomRate` int(11) NOT NULL, 
    `search_airportDist` int(11) NOT NULL, 
    `search_HotelSuite` tinyint(1) NOT NULL, 
    `search_VillaCondo` tinyint(1) NOT NULL, 
    `search_Amenities` text NOT NULL, 
    `active` tinyint(1) NOT NULL, 
    PRIMARY KEY (`resortID`) 
) 
+0

我们可以看到t的结构他表? –

+0

@BookOfZeus结构已添加到原始问题。对不起,一塌糊涂。 – MingMing

回答

4

你混合两种不同类型JOIN语法 - 在表中列出FROM的隐式类型,并明确JOIN类型。相反,尝试:

SELECT 
    islandID AS parentIslandID, 
    islandName, 
    island.longDesc, 
    imageLocation, 
    COUNT(r1.resortID) AS totalResorts, 
    r1.resortID 
FROM island 
    JOIN resort r1 ON island.islandID = r1.parentIslandID 
    JOIN images ON island.islandID = images.parentObjectID 
WHERE 
    r1.resortID IN ( 
    59,62,65,69,71,72,74,75,76,82,86,89,91,93,95,105, 
    106,116,117,118,120,121,122,123,124,125,126,127, 
    131,145,146,150,157,159,160,167,170,174,176,185,188,189,193, 
    194,198,199,200,203,205,213,217 
) 
AND resort.active = '-1' 
GROUP BY r1.parentIslandID 
ORDER BY totalResorts DESC 

**编辑,包括岛JOIN表结构公布后。

此外:

  • MySQL使用AND而非&&布尔和
  • 表的别名不需要AS关键字(JOIN resort r1
  • 务必在选择列表中使用您的resort别名r1r1.resortID
+0

我运行了您发布的代码,并且仍然弹出一条错误消息。在执行您发布的SQL时,出现“字段列表”中的“未知列'resort.resortID'”错误。有任何想法吗? – MingMing

+0

@MingMing什么错误?你是否也以最新的形式运行它?自发帖 –

+0

以来,我做了一些修改。我一直敲击回车,然后提交评论。 – MingMing

相关问题