dsfhd78978 2019-01-25 01:39
浏览 307
已采纳

如何使用laravel 5.7在内连接中连接表中的几个子选择

I have a query select that works very well directly in MySql, however, I would like to know if I have to replicate in Laravel 5.7, in the Controller class, to send it to my view and create a table with the data, follow the query:

SELECT
    B.ID,
    A.nome,
    A.vulgo,
    A.rg,
    A.sexo,
    I.indiciado_por,
    B.area_atuacao,
    C.carta,
    N.naipe,
    B.data_entrada,
    S.situacao,
    B.data_saida,
    B.observacoes
FROM
    baralho B
INNER JOIN baralho_naipe N ON
    B.id_naipe = N.naipe_id
INNER JOIN baralho_carta C ON
    B.id_carta = C.carta_id
INNER JOIN baralho_situacao S ON
    B.id_situacao = S.situacao_id
INNER JOIN(
    SELECT
        id_baralho,
        GROUP_CONCAT(indiciado.indicio SEPARATOR '/') AS indiciado_por
    FROM
        baralho_has_baralho_indicio
    INNER JOIN baralho B ON
        baralho_has_baralho_indicio.id_baralho = B.id
    INNER JOIN baralho_indicios indiciado ON
        baralho_has_baralho_indicio.id_baralho_indicio = indiciado.indicio_id
    GROUP BY
        B.id
    HAVING
        B.id = B.id
) AS I
ON
    B.id = I.id_baralho
INNER JOIN(
    SELECT
        id_baralho,
        ALVO.nome,
        ALVO.vulgo,
        ALVO.rg,
        ALVO.sexo
    FROM
        baralho_has_baralho_alvo
    INNER JOIN baralho B ON
        baralho_has_baralho_alvo.id_baralho = B.id
    INNER JOIN baralho_alvos ALVO ON
        baralho_has_baralho_alvo.id_baralho_alvo = ALVO.id_alvo
    GROUP BY
        B.id
    HAVING
        B.id = B.id
) AS A
ON
    B.id = A.id_baralho
ORDER BY
    B.id ASC  

How could I make this query work on Laravel? Please help me, I am a beginner at Laravel and thank you for any help

  • 写回答

1条回答 默认 最新

  • dougu3988 2019-01-25 05:25
    关注

    This will work I think

    DB::table('baralho AS B')
    ->join('baralho_naipe AS N', 'B.id_naipe', '=', 'N.naipe_id')
    ->join('baralho_carta AS C', 'B.id_carta', '=', 'C.carta_id')
    ->join('baralho_situacao AS S', 'B.id_situacao', '=', 'S.situacao_id')
    ->join(DB::raw("(SELECT
            id_baralho,
            GROUP_CONCAT(indiciado.indicio SEPARATOR '/') AS indiciado_por
        FROM
            baralho_has_baralho_indicio
        INNER JOIN baralho B ON
            baralho_has_baralho_indicio.id_baralho = B.id
        INNER JOIN baralho_indicios indiciado ON
            baralho_has_baralho_indicio.id_baralho_indicio = indiciado.indicio_id
        GROUP BY
            B.id
        HAVING
            B.id = B.id) AS I"), 'B.id', '=', 'I.id_baralho')
    ->join(DB::raw("(SELECT
            id_baralho,
            ALVO.nome,
            ALVO.vulgo,
            ALVO.rg,
            ALVO.sexo
        FROM
            baralho_has_baralho_alvo
        INNER JOIN baralho B ON
            baralho_has_baralho_alvo.id_baralho = B.id
        INNER JOIN baralho_alvos ALVO ON
            baralho_has_baralho_alvo.id_baralho_alvo = ALVO.id_alvo
        GROUP BY
            B.id
        HAVING
            B.id = B.id) AS A"), 'B.id', '=', 'A.id_baralho')
    ->orderBy('B.id',  'ASC')
    ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?