[英]Sql query Cross Transform or Pivot?
I have this query for MySql database:我对 MySql 数据库有以下查询:
SELECT
pim_pimcore_database.object_query_RGL.rulecode AS "rulecode",
pim_pimcore_database.object_query_RGL.DescrizioneRegola AS "rule_desc",
pim_pimcore_database.object_relations_RGL.position AS "id",
pim_pimcore_database.objects.o_classId,
pim_pimcore_database.objects.o_key,
pim_pimcore_database.object_collection_ruleIF_RGL.Operatore,
pim_pimcore_database.object_collection_ruleIF_RGL.Concatenatore
FROM
pim_pimcore_database.object_query_RGL
LEFT JOIN pim_pimcore_database.object_relations_RGL
ON pim_pimcore_database.object_query_RGL.oo_id =
pim_pimcore_database.object_relations_RGL.src_id
LEFT JOIN pim_pimcore_database.objects
ON pim_pimcore_database.object_relations_RGL.dest_id = pim_pimcore_database.objects.o_id
LEFT JOIN pim_pimcore_database.object_collection_ruleIF_RGL
ON pim_pimcore_database.object_relations_RGL.position =
pim_pimcore_database.object_collection_ruleIF_RGL.`index`
AND pim_pimcore_database.object_relations_RGL.src_id =
pim_pimcore_database.object_collection_ruleIF_RGL.o_id
GROUP BY
pim_pimcore_database.object_relations_RGL.position AS "ID"
WHERE
pim_pimcore_database.object_query_RGL.oo_id = 1042 AND
pim_pimcore_database.object_relations_RGL.ownername ="IfStatement"
That gives me the result:这给了我结果:
But what I need is table like this:但我需要的是这样的表:
How do I change the query for the desired output?如何更改所需 output 的查询?
Your cartesian product (doubling of the rows) appears to come from joining pim_pimcore_database.objects
on o_id.您的笛卡尔积(行数加倍)似乎来自加入pim_pimcore_database.objects
上的 pim_pimcore_database.objects。
If instead you represent this table twice in the query (join it twice) with relevant aliases and a predicate that selects only those kind of o_classid
relevant to that alias, then the cartesian will disappear and you'll get the result you seek:相反,如果您在查询中使用相关别名和仅选择与该别名相关的那种o_classid
的谓词在查询中表示该表两次(将其连接两次),那么笛卡尔将消失,您将获得您寻求的结果:
SELECT
...,
o_opt.key as OPT,
o_com.key as COM,
...
LEFT JOIN pim_pimcore_database.objects o_opt ON ... AND o_opt.o_classid = 'OPT'
LEFT JOIN pim_pimcore_database.objects o_com ON ... AND o_com.o_classid = 'COM'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.