如何通过使用内部联接和按条件分组来联接10个表

时间:2019-07-10 12:33:07

标签: mysql

我想将此查询转换为joins,这是我从经理那里得到的一项任务,请将下面的查询转换为Join格式。

    SELECT 
                               capder.cder_id,
                               capman.cman_name AS Manufacturer,
                               caprange.cran_name AS Range1,
                               capmod.cmod_name AS Model,
                               capmod.cmod_code AS ModelCode,
                               capder.cder_doors AS Doors,
                               nvdbodystyle.bs_description AS BodyStyle,
                               captrim.ctrim_name AS Trim1,
                               capder.cder_name as Trim2, 
                               capder.cder_drivetrain AS DriveTrain,
                               nvdmodelyear.MY_ref AS DerivativeModelYear,
                                  group_concat(distinct(nvddictonaryoption.DO_Description) order by nvddictonaryoption.DO_Description SEPARATOR '|') AS ExteriorColour

                      FROM   capman,
                               caprange,
                               capmod,
                               nvdbodystyle,
                               capder,
                               nvdmodelyear,
                               captrim,
                               nvdoptions,
                               nvddictonaryoption,
                               nvddictionarycategory
                      WHERE  capman.cman_code = caprange.cran_mantextcode
                      AND    caprange.cran_code = capmod.cmod_rancode
                      AND    nvdbodystyle.bc_code = capmod.cmod_bodystyle
                      AND    capmod.cmod_code = capder.cder_modcode
                      AND    capder.cder_id = nvdmodelyear.my_id 
                      AND    captrim.ctrim_code = capder.cder_trimcode 
                      AND    nvdoptions.OPT_Id = capder.cder_id
                      AND    nvdoptions.opt_optioncode = nvddictonaryoption.DO_OptionCode
                      AND    nvddictionarycategory.dc_catcode = nvddictonaryoption.do_catcode
                      AND    nvddictionarycategory.dc_cth_type = 'C'
                      AND   SUBSTR(nvdmodelyear.MY_ref,1,4) > 2006
                      group by cder_id,SUBSTR(nvdmodelyear.MY_ref,1,4),captrim.ctrim_name

MySQL工作台6.3 CE。 我这样尝试过

   SELECT 
                           cd.cder_id,
                           cm.cman_name AS Manufacturer,
                           cr.cran_name AS Range1,
                           cmd.cmod_name AS Model,
                           cmd.cmod_code AS ModelCode,
                           cd.cder_doors AS Doors,
                           nb.bs_description AS BodyStyle,
                           ct.ctrim_name AS Trim1,
                           cd.cder_name as Trim2, 
                           cd.cder_drivetrain AS DriveTrain,
                           nmy.MY_ref AS DerivativeModelYear,
                              group_concat(distinct(ndo.DO_Description) order by ndo.DO_Description SEPARATOR '|') AS ExteriorColour

                  FROM   capman cm
       inner join        caprange cr          on cm.cman_code = cr.cran_mantextcode
       inner join        capmod cmd           on cr.cran_code = cmd.cmod_rancode
       inner join        nvdbodystyle   nb    on nb.bc_code = cmd.cmod_bodystyle
       inner join        capder          cd   on cmd.cmod_code = cd.cder_modcode
       inner join        nvdmodelyear    nmy  on cd.cder_id = nmy.my_id 
       inner join        captrim          ct  on  ct.ctrim_code = cd.cder_trimcode 
       inner join        nvdoptions       nop   on nop.OPT_Id = cd.cder_id
       inner join        nvddictonaryoption ndo  on nop.opt_optioncode = ndo.DO_OptionCode
       inner join        nvddictionarycategory ndc on ndc.dc_catcode = ndo.do_catcode AND    ndc.dc_cth_type = 'C' AND   SUBSTR(nmy.MY_ref,1,4) > 2006
        group by cder_id,SUBSTR(nmy.MY_ref,1,4),ct.ctrim_name;
我是正确的吗?它给出正确的结果,但是执行和获取结果花费的时间太长。有没有更好的方法仅使用joins语法执行此查询,而我无权将索引放到表中

0 个答案:

没有答案