Laravel项目中怎么对数据库进行子查询
Admin 2022-06-21 群英技术资讯 352 次浏览
本项目中关联了2个数据库
'default' => env('DB_CONNECTION', 'mysql'), //默认使用mysql为连接库 'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => '192.168.0.xx', 'database' => 'database', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => 'tb_', 'strict' => false, ], 'mysql_snapshot' => [ 'driver' => 'mysql', 'host' => env('DB_HOST_SNAPSHOT', '192.168.0.xx'), 'database' => env('DB_DATABASE_SNAPSHOT', 'snapshot'), 'username' => env('DB_USERNAME_SNAPSHOT', 'root'), 'password' => env('DB_PASSWORD_SNAPSHOT', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => 'tb_', 'strict' => false, ], ],
在某个需求中,需要使用子查询获取snapshot快照表库的关联数据,从而实现以下sql逻辑
SELECT ... From (SELECT sum(game_count) AS sum_count, max(game_count) AS max_count, game_room_id, record_date FROM `tb_xx_snapshot` WHERE record_date BETWEEN '2017-05-17' AND '2017-05-23' AND type = '1' GROUP BY game_room_id) as main INNER JOIN `tb_xx_snapshot` AS `tb_gg` ON tb_gg.game_count = main.max_count and tb_gg.game_room_id = main.game_room_id where tb_gg.record_date BETWEEN '2017-05-17' AND '2017-05-23' AND tb_gg.type = '1' GROUP BY tb_gg.game_room_id;
其中子查询主要用到以下query builder语句
$query = DB::table('xx_snapshot')->where('xx','yy')->groupBy('xx'); $main = DB::connection('mysql_snapshot') ->table(DB::raw("({$query->toSql()}) as tb_main")) ->mergeBindings($query->getQuery()) // 绑定参数,否则sql语句会只有'?' ->get();
而join语句中可传入匿名函数重新构造,如再其中加多几个连接条件,或者查询条件
$con = DB::table('xx_snapshot') ->join('xx_snapshot as gg', function ($query) { $query->on('gg.game_count', '=', 'xx_snapshot.max_count') ->on('gg.game_room_id', '=', 'xx_snapshot.game_room_id') ->where('gg.xx','123'; })
实现上述需求完整代码如下:
$subQuery= GameroomModel::select(DB::raw('sum(game_count) as sum_count,max(game_count) as max_count,record_date,game_room_id')) ->whereBetween('record_date',[$beginDay,$endDay]) ->where('type','1') ->groupBy('game_room_id'); $main = DB::connection('mysql_snapshot') ->table(DB::raw("({$subQuery->toSql()}) as tb_main")) ->mergeBindings($playerGame->getQuery()) ->join('gameroom_snapshot as gg', function ($join) { $join->on('gg.game_count', '=', 'main.max_count') ->on('gg.game_room_id', '=', 'main.game_room_id'); }) - >select('main.max_count','main.sum_count','gg.record_date','main.game_room_id') ->whereBetween('gg.record_date',[$beginDay,$endDay]) ->groupBy('main.game_room_id') ->get();
代码中子查询和外层都group by了一次,应该可以再优化一下.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PHP中如果让字符串直接解析函数?下面本篇文章给大家分享一下PHP 字符串中直接解析函数的写法,希望对大家有所帮助!
今天小编就为大家分享一篇laravel 使用事件系统统计浏览量的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
怎么进行seata-php开发?本篇文章带大家聊聊seata-php开发指南,讲解一些前置知识,希望对大家有所帮助!
在本篇文章里小编给大家整理的是一篇关于php png失真的原因及解决办法,有需要的朋友们可以跟着学习参考下。
会话机制(Session)在PHP中用于保存并发访问中的一些数据。这使可以帮助创建更为人性化的程序,增加站点的吸引力。一个访问者访问你的web网站将被分配一个唯一的id...
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008