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. 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
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
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.