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

发布于 2024-09-29 20:06:40 字数 1362 浏览 4 评论 0原文

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

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 -“on 子句”中的未知列“accommodation.id” 我认为这是因为 FROM 子句中使用了另一个表。有人知道在查询多个表时如何使用 INNER JOIN 吗?

I have a site where a specific set of data is collected. This is a travel agency website. It is neccesary to determine whether or not an accommodation is still bookable. When I don't select any searchfilters (like destination, classification, facilities etc) I get a working query. The looks like:

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'

But when I select a filter (in this case a 'target' filter (children, active, rest, wintersports etc) I get the query:

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'

Now when I execute this query I get the error: #1054 - Unknown column 'accommodation.id' in 'on clause'. I think this is because another table is used in the FROM clausule. Does anyone have an idea on how to use the INNER JOIN when multiple tables are queried?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2024-10-06 20:06:40

完全过滤查询造成的困难......

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' 

The difficulity caused by a full filtered query...

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' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文