mysql - 如何将主查询值传递给子查询中的连接

时间:2013-09-19 15:12:36

标签: php mysql sql subquery visibility

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

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 - 'on clause'中的未知列'serv.idProject'

如果我删除linksidProject = servidProject我得到#1054 - 'on子句'中的未知列'serv.id ='。< / p>

基本上......我在ON子句中看不到来自serv的任何内容。如果我删除整个JOIN查询工作,那么在WHERE子句中serv是可见的。知道如何让它在ON子句中工作吗?

4 个答案:

答案 0 :(得分:3)

在内部选择中,您只加入subservlinks,因此您无法使用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`,'|') 
                )
...

应适用于内部子选择。

答案 1 :(得分: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别名在主体查询中定义,因此它应该可以访问subquerys。

答案 2 :(得分:3)

使用此http://sourceforge.net/projects/sqleo/来帮助您完成复杂的查询!

答案 3 :(得分:1)

  

子查询编译期间别名(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`