4

我有以下查询(嗯......让我烦恼的部分):

SELECT DISTINCT `serv`.`id`, `serv`.`name`, `prj`.`id` AS `idProject`, `prj`.`name` AS `projectName`, `units`.`id` AS `idUnit`,
    `units`.`name` AS `unitName`, `units`.`description` AS `unitDescription`, `units`.`descriptionLong` AS `unitLongDescription`,
    `units`.`address`, `units`.`telephone`, (
        SELECT DISTINCT GROUP_CONCAT(CONCAT(`subserv`.`id`, '|', `subserv`.`name`, '|', `links`.`continuous`))
        FROM `" . DBT_SERV . "` AS `subserv`
        JOIN `" . DBT_SERV_LINKS . "` AS `links` ON (
            `links`.`deleted` = 0 AND
            `links`.`idProject` = `serv`.`idProject` AND
            `links`.`mandatory` = '1' AND
            `links`.`subserviceLink` = 1 AND
            `links`.`serviceFrom` LIKE CONCAT ('|', `serv`.`id`,'|') AND
            `links`.`serviceTo` LIKE CONCAT('|', `subserv`.`id`, '|')
        )
        WHERE `idParentService` = `serv`.`id`
            AND `subserv`.`deleted` = 0
            AND `subserv`.`onSaleStatus` = 'onSale'
            AND `subserv`.`serviceGroup` = 'SS'
    ) AS `subservices`, (
        SELECT `file`
        FROM `" . DBT_GALLERY . "` AS `gallery`
        WHERE `gallery`.`category` = 'accommodationUnit'
            AND `idObject` = `idUnit`
        LIMIT 1
    ) AS `picture`
FROM `" . DBT_SERV . "` AS `serv`

ETC.,ETC ......更多的连接和位置。

我的问题是我收到以下错误:#1054 - Unknown column 'serv.idProject' in 'on Clause'

如果我删除 links. idProject= servidProject我得到#1054 - 'on 子句'中的未知列'serv.id ='。

serv基本上......我在 ON 子句中看不到任何东西。如果我删除整个 JOIN 查询工作,那么在 WHERE 子句serv中是可见的。知道如何使它在 ON 子句中也起作用吗?

4

3 回答 3

3

在内部选择中,您只是加入subservand links,因此您不能在ON. 但是,您应该能够在WHERE子句中访问外部表:

... (
SELECT DISTINCT GROUP_CONCAT(CONCAT(`subserv`.`id`, '|', `subserv`.`name`, '|', `links`.`continuous`))
                FROM `" . DBT_SERV . "` AS `subserv`
                JOIN `" . DBT_SERV_LINKS . "` AS `links` ON (
                    `links`.`deleted` = 0 AND
                    `links`.`mandatory` = '1' AND
                    `links`.`subserviceLink` = 1 AND
                    `links`.`serviceTo` LIKE CONCAT('|', `subserv`.`id`, '|')
                WHERE `links`.`idProject` = `serv`.`idProject` AND
                      `links`.`serviceFrom` LIKE CONCAT ('|', `serv`.`id`,'|') 
                )
...

应该适用于内部子选择。

于 2013-12-04T17:36:03.033 回答
3

尝试这个

SELECT DISTINCT `serv`.`SID`, `serv`.`SNAME`, `serv`.`SIDP`, `prj`.`id` AS `idProject`,
`prj`.`name` AS `projectName`, `units`.`id` AS `idUnit`,
`units`.`name` AS `unitName`, `units`.`description` AS `unitDescription`,
`units`.`descriptionLong` AS `unitLongDescription`, `units`.`address`, 
`units`.`telephone`,
    (
        SELECT DISTINCT GROUP_CONCAT(CONCAT(`subserv`.`id`, '|', `subserv`.`name`, '|', `links`.`continuous`))
        FROM `" . DBT_SERV . "` AS `subserv`
        JOIN `" . DBT_SERV_LINKS . "` AS `links` ON (
            `links`.`deleted` = 0 AND
            `links`.`idProject` = `serv`.`SIDP` AND
            `links`.`mandatory` = '1' AND
            `links`.`subserviceLink` = 1 AND
            `links`.`serviceFrom` LIKE CONCAT ('|', `serv`.`SID`,'|') AND
            `links`.`serviceTo` LIKE CONCAT('|', `subserv`.`id`, '|')
        )
        WHERE `idParentService` = `serv`.`SID`
            AND `subserv`.`deleted` = 0
            AND `subserv`.`onSaleStatus` = 'onSale'
            AND `subserv`.`serviceGroup` = 'SS'
    ) AS `subservices`, (
        SELECT `file`
        FROM `" . DBT_GALLERY . "` AS `gallery`
        WHERE `gallery`.`category` = 'accommodationUnit'
            AND `idObject` = `idUnit`
        LIMIT 1
    ) AS `picture`
FROM 
    (SELECT `id` AS `SID`, `idProject` AS `SIDP`, `name` AS `SNAME` FROM `" . DBT_SERV . "`) AS serv
ETC., ETC... some more joins and a where.

SID、SNAME 和 SIDP 别名在主体查询中定义,因此它应该可以被子查询访问。

于 2013-12-04T17:38:33.430 回答
1

alias(serv) 在子查询编译期间不存在,因为在子查询被认为有效并且其输出已知之前无法编译外部查询。

所以快速的解决方案就是在你的子查询中用 DBT_SERV 替换 serv 。

SELECT DISTINCT `serv`.`id`, `serv`.`name`, `prj`.`id` AS `idProject`, `prj`.`name` AS `projectName`, `units`.`id` AS `idUnit`,
    `units`.`name` AS `unitName`, `units`.`description` AS `unitDescription`, `units`.`descriptionLong` AS `unitLongDescription`,
    `units`.`address`, `units`.`telephone`, (
        SELECT DISTINCT GROUP_CONCAT(CONCAT(`subserv`.`id`, '|', `subserv`.`name`, '|', `links`.`continuous`))
        FROM `" . DBT_SERV . "` AS `subserv`
        JOIN `" . DBT_SERV_LINKS . "` AS `links` ON (
            `links`.`deleted` = 0 AND
            `links`.`idProject` = `" . DBT_SERV . "`.`idProject` AND
            `links`.`mandatory` = '1' AND
            `links`.`subserviceLink` = 1 AND
            `links`.`serviceFrom` LIKE CONCAT ('|', `" . DBT_SERV . "`.`id`,'|') AND
            `links`.`serviceTo` LIKE CONCAT('|', `subserv`.`id`, '|')
        )
        WHERE `idParentService` = `" . DBT_SERV . "`.`id`
            AND `subserv`.`deleted` = 0
            AND `subserv`.`onSaleStatus` = 'onSale'
            AND `subserv`.`serviceGroup` = 'SS'
    ) AS `subservices`, (
        SELECT `file`
        FROM `" . DBT_GALLERY . "` AS `gallery`
        WHERE `gallery`.`category` = 'accommodationUnit'
            AND `idObject` = `idUnit`
        LIMIT 1
    ) AS `picture`
FROM `" . DBT_SERV . "` AS `serv`
于 2013-12-07T06:11:40.230 回答