从子查询mysql未知列的子查询

时间:2014-04-09 14:18:40

标签: mysql sql performance select subquery

我有以下查询

SELECT
a.*, count(
    (SELECT matches.id FROM (
        SELECT
            p.id,
            IF ( p.verdeelbaar = '1', MIN(s.minopp), SUM(s.minopp) ) AS min,
            SUM( s.maxopp ) AS max,
            samenstelling_type_id,
            samenstelling_type_absolute_parent(samenstelling_type_id) AS top_type
        FROM
            app_pand AS p
        LEFT JOIN
            app_samenstelling AS s
        ON
            p.id = s.pand_id
        WHERE
            s.samenstelling_beschikbaarheid_id NOT IN (12,13)
        AND
            s.actief = 1
        AND 
            IF(a.hk!='te huur/te koop',hk = a.hk,TRUE)
        GROUP BY
            pand_id, top_type
        HAVING
            CASE top_type
                WHEN @kantoor THEN
                    min < a.kantoor_max AND max > a.kantoor_min
                WHEN @magazijn THEN
                    min < a.magazijn_max AND max > a.magazijn_min
                WHEN @terrein THEN
                    min < a.terrein_max AND max > a.terrein_min
            END

        ) AS matches
    GROUP BY matches.id )
) AS m
FROM
app_aanvragen AS a
WHERE a.id = @aanvraag;

将此查询设为我想要计算一个aanvraag (the dutch word for a request)的匹配数。

问题是,在最内部的查询中,来自FROM的{​​{1}}不知道matches记录a

我知道很多关于这个问题的解决方案都说你必须加入两个表格,这样才能到达(a.hk, a.kantoor, a.magazijn..)个记录,但我不知道如何加入它们,因为它们没有连接条件。

这个查询首先是否可行,若然,我该怎么做?如果没有,是否有人看到这个问题的另一种解决方案?

提前Thanx!

0 个答案:

没有答案