我的数据库包含默认字段表和自定义字段的自定义表。而主表有一个id,自定义的id有id_c(来自id的外键)。 我的表ger01_gerenciamentocliente_contacts_c用于联系人ID和ger01 ... id的2个外键连接 他们的关系是联系人1 - n ger01_gerenciamentocliente
我得到每个ger01_gerenciamentocliente的分钟,但我需要每个联系人ID的分钟(日期)(来自ger01)。我该怎么做?
SELECT T3.id, T0.MIN_MONTH FROM
(SELECT id_c, MIN(lftm_mes_referencia_c) as MIN_MONTH
FROM ger01_gerenciamentocliente_cstm GROUP BY id_c) T0
INNER JOIN ger01_gerenciamentocliente T1 ON T0.id_c = T1.id AND T1.deleted = 0
INNER JOIN ger01_gerenciamentocliente_contacts_c T2 ON T1.id = T2.ger01_gere60bccliente_idb AND T2.deleted = 0
INNER JOIN contacts T3 ON T3.id = T2.ger01_gerenciamentocliente_contactscontacts_ida AND T3.deleted = 0
WHERE T0.MIN_MONTH IS NOT NULL ORDER BY T3.id ASC;
答案 0 :(得分:0)
我现在正确地检索了sql
SELECT T3.id, MIN(T0.lftm_mes_referencia_c) as reference FROM
ger01_gerenciamentocliente_cstm T0
INNER JOIN ger01_gerenciamentocliente T1 ON T0.id_c = T1.id AND T1.deleted = 0
INNER JOIN ger01_gerenciamentocliente_contacts_c T2 ON T1.id = T2.ger01_gere60bccliente_idb AND T2.deleted = 0
INNER JOIN contacts T3 ON T3.id = T2.ger01_gerenciamentocliente_contactscontacts_ida AND T3.deleted = 0
INNER JOIN contacts_cstm T4 ON T3.id = T4.id_c AND T4.deleted = 0
WHERE T0.lftm_mes_referencia_c IS NOT NULL GROUP BY T3.id;