来自相同列的两个不同值作为单独的列返回相同的结果

时间:2015-06-19 07:09:26

标签: mysql sql

我有一个查询,我将一列中的2个值作为两个单独的列,每个列都有不同的值。我的查询为这两列返回相同的值。 但我需要为每个人获得不同的价值观。第一个“劳动强度”返回“需求ID”的结果,并且获得正确的值。

我已在前面用*标记了这两列。

select
 document.id AS API_ID,
 document.name AS NAME,
 document.description AS DESCRIPTION,
 document.documentKey AS DOCUMENT_KEY,
 docs.sequence AS SEQUENCE,
 documentcustomfieldvalue.documentId AS DOCUMENT_ID,
 *MAX(CASE WHEN documenttypefielddefinition.label = 'Labor Intensity' THEN textValue ELSE NULL END) AS DARBIETILPIBA,
 *MAX(CASE WHEN documenttypefielddefinition.label = 'Requirements ID' THEN textValue ELSE NULL END) AS PRASIBAS_ID,
from document
join (select doc.id, doc.name, dn.sequence, dn.globalSortOrder
      from document doc, documentnode dn
      where doc.projectId = 20249 -- report_projectId
        and dn.scopeId = 5
        and dn.refId = doc.id
        and dn.baseLineId is null
        and dn.sequence like '2%') docs on docs.id = document.id
join project on document.projectId= project.id
left join documentcustomfieldvalue on documentcustomfieldvalue.documentId = document.id

left join documenttype on document.documentTypeId = documenttype.id
left join documenttypefielddefinition on documenttype.id = documenttypefielddefinition.documentTypeId
left join documentfield on documenttypefielddefinition.documentFieldId = documentfield.id
left join userbase on documentcustomfieldvalue.textValue = userbase.id

where document.projectId = project.id
and document.active = 'T' and documenttypefielddefinition.label IN ('Labor Intensity','Requirements ID')
group by docs.sequence
order by docs.globalSortOrder

将该值视为重复的错误是什么?如果我只在where语句中留下“劳动强度”,那么它仍然会给出“劳动强度”值的“要求ID”值。

如果您无法理解某些内容,请让我澄清一下。 谢谢!

1 个答案:

答案 0 :(得分:0)

我弄清楚出了什么问题。一个左连接是错误的,需要额外的设置。

正在运行的当前左连接。

left join documenttype on document.documentTypeId = documenttype.id
left join documenttypefielddefinition on documenttype.id = documenttypefielddefinition.documentTypeId
left join documentfield on documenttypefielddefinition.documentFieldId = documentfield.id
left join documentcustomfieldvalue on document.id = documentcustomfieldvalue.documentId 
and documentfield.id = documentcustomfieldvalue.fieldId
left join userbase on documentcustomfieldvalue.textValue = userbase.id
相关问题