doudouxuqh198138 2019-08-14 07:23
浏览 79

如何在Laravel 5.8中写这个(左连接,然后打开)?

I cannot write my query in Laravel query builder, could help me?

I try to write but display many error.

select tb.id, `tb`.insert,
   `tb`.update,
   `tb`.delete,
   `tb`.list,
   `permissions`.`name`,
   `permissions`.`id` as permission_id
   from  `permissions`
   left join (select * from `role_permissions` where `role_permissions`.`role_id` = ' . $id . ') tb on `permissions`.id = tb.permission_id
        order by `permissions`.`id` asc'
  • 写回答

1条回答 默认 最新

  • doulin6088 2019-08-14 09:27
    关注

    As I wrote in my comment:

    Line 8: you forgot to add the as behind the tb. Also, a subselect can only have one column returned from it, so you would need one subselect for each column that you would want returned from the model table.

    Try to use this code here:

    select tb.id,
        `tb`.insert,
        `tb`.update,
        `tb`.delete,
        `tb`.list,
        `ps`.`name`,
        `ps`.`id` as permission_id
        from  `permissions` as ps
        left join (select `permission_id` from `role_permissions` where `role_permissions`.`role_id` = ' . $id . ') as tb on `ps`.id = tb.permission_id
        order by `ps`.`id` asc'
    

    This Code is for pure PHP. Laravel is using a "special" notation. How you can write a simple SQL Query in Laravel you can see here and here.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大