在选择多个表时使用INNER JOIN和MySQL?

时间:2010-10-27 21:36:22

标签: sql mysql join mysql-error-1054

我有一个网站,其中收集了一组特定的数据。这是一个旅行社网站。确定住宿是否仍然可预订是必要的。当我没有选择任何搜索过滤器(如目的地,分类,设施等)时,我得到了一个有效的查询。看起来像:

SELECT `accommodation` . *
FROM `accommodation`
INNER JOIN (

SELECT `fk_accommodation` , MAX( `dateuntil` ) - ( `releasedays` *60 *60 *24 ) AS `LatestBookableTimestamp`
FROM `priceperiod`
GROUP BY `fk_accommodation`
) AS `pp` ON ( `pp`.`fk_accommodation` = `accommodation`.`id` )
WHERE `pp`.`LatestBookableTimestamp` > UNIX_TIMESTAMP( )
AND `accommodation`.`fk_country` <>0
AND `accommodation`.`classification` >=0
AND `accommodation`.`type` = 'Z'

但是当我选择一个过滤器(在这种情况下是'目标'过滤器(儿童,活动,休息,冬季运动等)时,我得到了查询:

SELECT `accommodation` . *
FROM `accommodation` , `link_at`
INNER JOIN (

SELECT `fk_accommodation` , MAX( `dateuntil` ) - ( `releasedays` *60 *60 *24 ) AS `LatestBookableTimestamp`
FROM `priceperiod`
GROUP BY `fk_accommodation`
) AS `pp` ON ( `pp`.`fk_accommodation` = `accommodation`.`id` )
WHERE `pp`.`LatestBookableTimestamp` > UNIX_TIMESTAMP( )
AND `accommodation`.`fk_country` <>0
AND `link_at`.`fk_target`
IN ( 10, 2 )
AND `link_at`.`fk_accommodation` = `accommodation`.`id`
AND `accommodation`.`classification` >=0
AND `accommodation`.`type` = 'Z'

现在,当我执行此查询时,我收到错误:#1054 - Unknown column 'accommodation.id' in 'on clause'。我认为这是因为FROM clausule中使用了另一个表。当查询多个表时,有没有人知道如何使用INNER JOIN?

1 个答案:

答案 0 :(得分:0)

完整过滤查询导致的难度......

SELECT 
    `accommodation`.* 
FROM 
    `accommodation` , 
    `link_at` , 
    `priceperiod` , 
    `link_af` 
INNER JOIN ( 
    SELECT 
        `fk_accommodation`, 
        MAX( `dateuntil` ) - ( `releasedays` *60 *60 *24 ) AS `LatestBookableTimestamp` 
    FROM 
        `priceperiod` 
    GROUP BY 
        `fk_accommodation` 
    ) AS `pp` 
    ON (`pp`.`fk_accommodation` = `accommodation`.`id`) 
WHERE 
    `pp`.`LatestBookableTimestamp` > UNIX_TIMESTAMP() 
AND 
    `accommodation`.`fk_country` <> 0 
AND 
    `link_at`.`fk_target` IN (10 , 2 , 1 , 13 , 6 , 3) 
AND 
    `link_at`.`fk_accommodation` = `accommodation`.`id` 
AND 
    ( 
        ( 
            `priceperiod`.`haslogies` = '1' 
        AND 
            ( `datefrom` >= 1288216378 
        OR 
            `dateuntil` <= 1288216378 
            )
         AND 
            `accommodation`.`id` = `priceperiod`.`fk_accommodation` 
        ) 
        OR 
        (
             `priceperiod`.`haslogiesbreakfast` = '1' 
        AND 
        ( 
            `datefrom` >= 1288216378 
        OR 
            `dateuntil` <= 1288216378 
        ) 
        AND 
            `accommodation`.`id` = `priceperiod`.`fk_accommodation` 
        ) 
        OR 
        ( 
            `priceperiod`.`hashalfpension` = '1' 
        AND 
        ( 
            `datefrom` >= 1288216378 
        OR 
            `dateuntil` <= 1288216378 
        ) 
        AND 
        `accommodation`.`id` = `priceperiod`.`fk_accommodation` 
    ) 
    OR 
    ( 
        `priceperiod`.`hasfullpension` = '1' 
        AND 
        ( 
            `datefrom` >= 1288216378 
        OR 
            `dateuntil` <= 1288216378 
        ) 
        AND 
        `accommodation`.`id` = `priceperiod`.`fk_accommodation` 
    ) 
    OR 
    ( 
        `priceperiod`.`hasallinclusive` = '1' 
        AND 
        ( 
            `datefrom` >= 1288216378 
        OR 
            `dateuntil` <= 1288216378 
        ) 
        AND 
            `accommodation`.`id` = `priceperiod`.`fk_accommodation` 
        ) 
    ) 
    AND 
        `accommodation`.`id` IN 
        (
        SELECT 
            `fk_accommodation` 
        FROM 
            `link_af` 
        WHERE 
        (
            `fk_facility` = 13 OR 
            `fk_facility` = 14 OR 
            `fk_facility` = 7 OR 
            `fk_facility` = 27 OR 
            `fk_facility` = 37 OR 
            `fk_facility` = 17 OR 
            `fk_facility` = 24 OR 
            `fk_facility` = 3
        ) 
        GROUP BY 
            `fk_accommodation` 
        HAVING count( fk_accommodation ) =8) 
        AND 
        ( 
            ( 
                `accommodation`.`fk_accommodationtype` = 14 
            ) 
            OR 
            ( 
                `accommodation`.`fk_accommodationtype` = 18 
            ) 
            OR 
            ( 
                `accommodation`.`fk_accommodationtype` = 16 
            ) 
            OR 
            ( 
                `accommodation`.`fk_accommodationtype` = 19 
            ) 
            OR 
            (
                 `accommodation`.`fk_accommodationtype` = 17 
            ) 
        ) 
        AND 
        (
            `accommodation`.`isspecialoffer` = 1 
        OR 
            `accommodation`.`istip` = 1
        ) 
        AND 
            `accommodation`.`classification` >= 4 
        AND `accommodation`.`type` = 'Z'