简体   繁体   English

子查询引用外部列的问题

[英]Issue with subquery referencing outer columns

I looked searched through the existing answers and couldn't quite find what I am looking for. 我看了看现有的答案,却找不到我想要的东西。 When I run my query it is telling me: "Unknown column mi1.id in where clause" I'm sure I'm just not referring to it the correct way. 当我运行查询时,它告诉我:“ where子句中的未知列mi1.id”,我确定我只是没有正确地引用它。 Here is what I have: 这是我所拥有的:

SELECT me1.id AS id
    ,me1.id AS me_id
    ,mi1.id AS mi_id
    ,ci.id AS ci_id
    ,mi1.first_name AS first_name
    ,mi1.last_name AS last_name
    ,(
        SELECT MAX(max_date)
        FROM (
            SELECT MAX(mi2.last_updated_dts) AS max_date
            FROM member_info AS mi2
            WHERE mi2.id = mi1.id --I think this is the issue 

            UNION ALL

            SELECT MAX(lt.created_dts)
            FROM live_training AS lt
            WHERE lt.me_id = me1.me_id

            UNION ALL

            SELECT MAX(gm.created_dts)
            FROM group_member AS gm
            WHERE gm.me_id = me1.me_id

            UNION ALL

            SELECT MAX(clm.created_dts)
            FROM contact_list_member AS clm
            WHERE clm.me_id = me1.me_id

            UNION ALL

            SELECT MAX(mc.created_dts)
            FROM member_case AS mc
            WHERE mc.me_id = me1.me_id

            UNION ALL

            SELECT MAX(mcc.created_dts)
            FROM member_case_comment AS mcc
            INNER JOIN member_case AS mc ON (mcc.member_case_id = mc.id)
            WHERE mc.me_id = me1.me_id
            ) AS t
        ) AS last_interaction_date
FROM member_info AS mi1
INNER JOIN member_enterprise AS me1 ON (me1.member_id = mi1.id)
LEFT JOIN customer_info AS ci ON (ci.id = mi1.customer_info_id)
WHERE me1.ent_id = 3
GROUP BY me1.id

member_info member_info

    id  bigint(20) AUTO_INCREMENT
    first_name  varchar(100)
    last_name   varchar(100)    
    email   varchar(255)
    registration_dts    timestamp
    last_updated_dts    timestamp

I just did some testing. 我只是做了一些测试。 the problem is the depth level of inner select 问题是内部选择的深度级别

SqlFiddleDemo SqlFiddleDemo

You can do this: 你可以这样做:

SELECT
  productName,
  description,
  (SELECT MAX(description)
   FROM  ForgeRock fr2
   WHERE fr2.productName <> fr1.productName   
  ) as newDescription
FROM
  ForgeRock fr1;

But not this 但这不是

SELECT
  productName,
  description,
  (SELECT MAX(description)
   FROM (SELECT description
         FROM ForgeRock fr2
         WHERE fr2.productName <> fr1.productName
         ) as A
  ) as newDescription
FROM
  ForgeRock fr1;

Move the gigantic union subquery out of the select list into the from list to form a derived table and join member_info on this derived table. 将巨大的并集子查询从选择列表中移到从列表中,以形成派生表,并在此派生表上加入member_info。

SELECT
mi1.first_name AS first_name,
mi1.last_name AS last_name,
t2.max_max_date
FROM
member_info as mi1 
INNER JOIN member_enterprise as me1 on(me1.member_id = mi1.id) 
LEFT JOIN customer_info as ci on(ci.id = mi1.customer_info_id)
LEFT JOIN     (SELECT id, MAX(max_date) as max_max_date FROM    
    (   SELECT mi2.id as id, MAX(mi2.last_updated_dts) AS max_date 
        FROM member_info AS mi2
        GROUP BY mi2.id 
     UNION ALL 
        SELECT lt.me_id,MAX(lt.created_dts) 
        FROM live_training as lt 
        GROUP BY lt.me_id 
     UNION ALL 
        SELECT gm.me_id, MAX(gm.created_dts) 
        FROM group_member as gm 
        GROUP BY gm.me_id 
     UNION ALL 
        SELECT clm.me_id, MAX(clm.created_dts) 
        FROM contact_list_member as clm 
        GROUP BY clm.me_id 
     UNION ALL 
        SELECT mc.me_id, MAX(mc.created_dts) 
        FROM member_case as mc 
        GROUP BY mc.me_id 
     UNION ALL 
        SELECT mc.me_id, MAX(mcc.created_dts) 
        FROM member_case_comment as mcc INNER JOIN member_case as mc ON (mcc.member_case_id = mc.id) 
        GROUP BY mc.me_id
     ) as t
GROUP BY Id
) as t2
ON t2.Id=me1.me_id
WHERE me1.ent_id = 3
GROUP BY me1.id, mi1.first_name, mi1.last_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM