php laravel左连接leftJoin多条where语句
通常情况下我们在做leftjoin连接时需要对不止一个条件进行进行匹配,这时候就需要使用闭包方式,如下:
leftjoin('db', function ($join) {···});
leftjoin多条件查询,无非以下三种情况。
- 并且关系(&&)且为字段名称,使用on,代码示例如下:
$roomUuid = 1;
$chatInfo = DB::table('chat_info')
->where('chat_info.room_uuid', $roomUuid)
->leftJoin('user_rooms', function ($join) {
$join->on('user_rooms.user_uuid', '=', 'chat_info.user_uuid')
->on('user_rooms.room_uuid', '=', 'chat_info.room_uuid');
})
- 或者关系(||),将on改为orOn,代码示例如下:
$roomUuid = 1;
$chatInfo = DB::table('chat_info')
->where('chat_info.room_uuid', $roomUuid)
->leftJoin('user_rooms', function ($join) {
$join->on('user_rooms.user_uuid', '=', 'chat_info.user_uuid')
->orOn('user_rooms.room_uuid', '=', 'chat_info.room_uuid');
})
- 多条件查询,使用where,并使用use传递参数,代码示例如下:
$roomUuid = 1;
$chatInfo = DB::table('chat_info')
->where('chat_info.room_uuid', $roomUuid)
->leftJoin('user_rooms', function ($join) use ($chatInfo) {
$join->on('user_rooms.user_uuid', '=', 'chat_info.user_uuid')
->where('user_rooms.room_uuid', '=', $chatInfo);
})
php laravel左连接leftJoin多条where语句的更多相关文章
- linq join 左连接 leftjoin 多个on条件 where 条件
var haveChange = from newScore in newScoreList join oldScore in oldScoreList on new{newScore.ExamId, ...
- .net一次连接执行多条sql语句
方法一: string SQLString="select 1; select 2;"; using (OdbcConnection connection = new OdbcCo ...
- Linq Left Join;linq左连接 (转载)
来源 https://www.cnblogs.com/xinjian/archive/2010/11/17/1879959.html 准备一些测试数据,如下: use Test Create tabl ...
- linq to entity 左连接 右连接 以及内连接写法的区别
左连右连还是内连这个其实你不需要关心.只需要根据实体的映射关系写查询,框架会自动帮你生成的. 至于linq查询语法与扩展方法的效率,应该是一样的,比如: var users=(from u in db ...
- MySQL左连接时 返回的记录条数 比 左边表 数量多
在学MySQL的连接时,为了便于记忆,就将左连接 记做 最后结果的总记录数 和 进行左连接的左表的记录数相同,简单的说就是下面这个公式 count(table A left join table B) ...
- mysql 内连接、左连接、右连接
记录备忘下,初始数据如下: DROP TABLE IF EXISTS t_demo_product; CREATE TABLE IF NOT EXISTS t_demo_product( proid ...
- Linq连接查询之左连接、右连接、内连接、全连接、交叉连接、Union合并、Concat连接、Intersect相交、Except与非查询
内连接查询 内连接与SqL中inner join一样,即找出两个序列的交集 Model1Container model = new Model1Container(); //内连接 var query ...
- LINQ的左连接、右连接、内连接
.左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals ...
- SQL Server中的连接查询【内连接,左连接,右连接,。。。】
在查询多个表时,我们经常会用“连接查询”.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 什么是连接查询呢? 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据 ...
随机推荐
- CSS3总结五:弹性盒子(flex)、弹性盒子布局
弹性盒子容器的属性与应用 display:flex/inline-flex flex-direction flex-wrap justify-content align-items align-con ...
- 微信小程序富文本
<div class="weui-panel__bd pad-all fs13 " > <rich-text nodes="{{detail.conte ...
- 6 java 笔记
1 java的类通过构造器来创建该类的对象 2 java提供extends关键字来实现子类继承父类 3 初始化块总是在构造器调用之前被执行 4 可以吧java中的类当成一种自定义的类型 5 类定义的变 ...
- 用 Portainer 远程管理 docker
参考的官网地址为:https://portainer.readthedocs.io/en/stable/deployment.html 先更新Centos docker 镜像加速地址: curl -s ...
- 第五章· MySQL数据类型
一.数据类型介绍 1.四种主要类别  1)数值类型 2)字符类型 3)时间类型 4)二进制类型 2.数据类型的 ABC 要素 1)Appropriate(适当) 2)Brief(简洁) 3)Comp ...
- string类的总结
一.string类头文件:#include <string>;using namespace std; 二.string类方法: 1.获取string的字符串长度:size(),size返 ...
- B+(B)树和B-树
转载自 http://www.cnblogs.com/nullzx/ 1.B树 定义:B树也称B-树,它是一颗多路平衡查找树.我们描述一颗B树时需要指定它的阶数,阶数表示了一个结点最多有多少个孩子结点 ...
- java线程基础巩固---多线程与JVM内存结构的关系及Thread构造函数StackSize的理解
继续学习一下Thread的构造函数,在上次[http://www.cnblogs.com/webor2006/p/7760422.html]已经对如下构造都已经学习过了: 多线程与JVM内存结构的关系 ...
- httpwatch
https://blog.csdn.net/coderising/article/details/89530016 别人的文章
- 一秒钟解决mysql使用游标出现取值乱码问题
drop procedure if exists pro_test; delimiter // create procedure pro_test() begin declare str varcha ...