在包含变量的子查询中访问父查询值

时间:2012-07-31 14:28:56

标签: mysql subquery

我已经做了很多关于这方面的工作,并希望在我们采取不同的策略之前用尽所有可能性。

以下查询:

SELECT 
    node.nid AS nid
FROM
    node node
WHERE
    (node.type in ('new_catalog_master')) 
AND (SELECT 
        status
    FROM
        (SELECT 
            @COST := (SELECT 
                    pm.field_price_matrix_ppu_value
                FROM
                    node n
                INNER JOIN content_type_new_catalog_master ncm ON ncm.nid = n.nid
                INNER JOIN content_type_vendor_price_object vpo ON vpo.field_vpo_product_ref_nid = ncm.nid
                INNER JOIN content_type_price_matrix pm ON pm.nid = vpo.nid
                WHERE
                    ncm.nid = node.nid AND vpo.field_vpo_pref_vendor_value = 'yes' AND pm.field_price_matrix_quantity_value = (SELECT 
                        MAX(ctpm.field_price_matrix_quantity_value)
                    FROM
                        content_type_price_matrix ctpm
                    WHERE
                        ctpm.nid = vpo.nid)),
                @DATA_MARGIN := (SELECT 
                    cs.low
                FROM
                    competitor_stats cs
                WHERE
                    cs.nid = node.nid),
                @SUGGESTED_MARGIN := (1-(@COST/@DATA_MARGIN))*100,
                @MARGIN := (SELECT 
                    ctfmm.field_margin_matrix_gross_margin_value, ctfmm.field_margin_matrix_role_value 
                FROM
                    content_type_flexi_margin_matrix ctfmm
                WHERE
                    ctfmm.nid = node.nid AND ctfmm.field_margin_matrix_role_value = 1),
                @MARGIN_MAX := 5/100,
                (ABS(@SUGGESTED_MARGIN-@MARGIN) > @SUGGESTED_MARGIN*@MARGIN_MAX) AS status
        ) as result);

返回:

Unknown column 'node.nid' in 'where clause'

如果我们将nid值硬编码到子查询中,查询将返回一个值。是否可以访问子查询中的node.nid?

由于某些限制,我们需要将子查询保留在WHERE子句中。

0 个答案:

没有答案