我查看了现有的答案,但找不到我要找的东西。当我运行我的查询时,它告诉我:“where where子句中的未知列mi1.id”我确信我只是没有用正确的方式来指代它。这就是我所拥有的:
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
id bigint(20) AUTO_INCREMENT
first_name varchar(100)
last_name varchar(100)
email varchar(255)
registration_dts timestamp
last_updated_dts timestamp
答案 0 :(得分:0)
我刚做了一些测试。问题是内部选择的深度水平
<强> SqlFiddleDemo 强>
你可以这样做:
SELECT
productName,
description,
(SELECT MAX(description)
FROM ForgeRock fr2
WHERE fr2.productName <> fr1.productName
) as newDescription
FROM
ForgeRock fr1;
但不是这个
SELECT
productName,
description,
(SELECT MAX(description)
FROM (SELECT description
FROM ForgeRock fr2
WHERE fr2.productName <> fr1.productName
) as A
) as newDescription
FROM
ForgeRock fr1;
答案 1 :(得分:0)
将巨大的联合子查询从选择列表中移出到from列表中,以形成派生表,并在此派生表上连接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