mysql - 如何在 laravel 中编写 sql 连接查询

标签 mysql laravel laravel-5

I have the following working SQL query and it is working fine

我已经在数据库(SQL 编辑器)中测试过

select b.name "group_id", b.name "group_name",bt.name "group_type",a.location_name ,b.total_users ,group_concat(u.full_name) "members",group_concat(ui.picture_1) "pictures",(select picture_1 from user_pictures where id IN 
(select uii.profile_id from users as uii where uii.id = b.owner_id )) as owner_images
from users u
inner join groups b
inner join group_types bt
inner join group_users bu
inner join addresses a
inner join user_pictures ui
on
u.id=bu.user_id and
b.id=bu.group_id and
bt.id=b.type_id and
a.id=b.address_id and
ui.id =u.profile_id and
bu.status="active" and bu.user_id=3 and b.owner_id <> 3
group by b.name, bt.name, bu.status

SAMPLE OUTPUT

enter image description here

问题:我想将它转换成 Laravel 格式。

I tried the following getting everything except the pictures field

$groupInfo =  DB::table('users as u')                     
                                ->join('group_users as bu', 'bu.user_id', '=', 'u.id')
                                ->join('groups as b', 'b.id', '=', 'bu.group_id')
                                ->join('group_types as bt', 'bt.id', '=', 'b.type_id')
                                ->join('addresses as a', 'a.id', '=', 'b.address_id')  
                                ->join('user_images as ui', 'ui.id', '=', 'u.profile_id')             
                                ->where('bu.status', '=', 'active')
                                ->where('bu.user_id', '=', $user_id)
                                ->where('b.owner_id', '<>', $user_id)
                                ->groupBy('b.name')
                                ->groupBy('bt.name')
                                ->groupBy('bu.status')
                                ->select('b.id as group_id','b.name as group_name','bt.name as group_type','b.owner_id','a.location_name','b.total_users',DB::raw('group_concat(u.id) as members'),DB::raw('group_concat(ui.image_1) as images'))
                                ->get();

        return $groupInfo;  

How to add following sql inner query in the above laravel query?

(select picture_1 from user_pictures where id IN 
(select uii.profile_id from users as uii where uii.id = b.owner_id )) as owner_pictures

如何执行查询?

最佳答案

这是示例。您可以编辑更多内容并使用此查询

$results= \DB::table('groups as b')
                ->join('group_types as bt', function($join) {
                    $join->on('bt.id', '=', 'b.type_id')
                         ->where('b.owner_id', '<>', 3);
                })
                ->join('group_users as bu', function($join) {
                    $join->on('b.id', '=', 'bu.group_id')
                         ->where('bu.status', '=', 'active');
                })
                ->select('b.name','bt.name')
                ->groupBy('b.name, bt.name')                    
                ->get();

关于mysql - 如何在 laravel 中编写 sql 连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36262734/

相关文章:

php - 拉维尔 5 : How to retrieve records according to date?

mysql - 将具有 rowid 和 rownum 的 Oracle 查询转换为 MySQL 查询

php - 加入或添加专栏?

mysql - 搜索多列返回最多匹配项

php - 在 PHP 中显示 MySQL 数据库中的类别和子类别

php - 在 Laravel 中将旧表迁移到新表

laravel - 为 Mail::send() 方法定义电子邮件地址

php - 消息为 '... must return a relationship instance.' 的 LogicException

php - Laravel 不显示 index.php

php - 在 laravel 5 中使用 bootstrap 进行多级导航