sql查询交叉转换还是pivot?

idfiyjo8  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(300)

我对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"

这给了我这个结果

但我需要一张不同的table,比如

有人能帮我吗?先谢谢你

flvtvl50

flvtvl501#

您的笛卡尔积(行数加倍)似乎来自连接 pim_pimcore_database.objects 在o\ U id上。
相反,如果您在查询中用相关别名和仅选择这些类型的 predicate 表示该表两次(联接两次) 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'

相关问题