环境:Laravel 5.7.28数据库 mysqlCentOS 7.2我有 3 个表,如下所示,我需要加入这 3 个表并合并列 ( customer.first_name,customer.last_name,customer.address,job.name,job.address,job.date) 来设置“喜欢”查询。例如,在 coulms 合并customer.first_name,customer.last_name,customer.address,job.name,job.address,job.date customer.first_name + customer.last_name + customer.address + job.name + job.address +job.date 之后是 'TOMSMITHCecilia ChapmanABC.LtdIris Watson2019-01-10',所以 when $text = 'MS';set 'like' '%'.$text.'%' will return below resultcustomer.first_name = TOMcustomer.last_name = SMITHcustomer.address = Cecilia Chapmanjob.name = ABC.Ltdjob.address = Iris Watsonjob.date = 2019-01-10id 表(关系属于表客户和工作)ID顾客ID作业编号客户表ID名姓地址工作编号ID姓名地址工作日期
2 回答
森林海
TA贡献2011条经验 获得超2个赞
看到这个
\DB::table('id')->join('customer','customer.customer_id','id.customer_id')
->join('job','job.id','id.job_id')
->where('customer.first_name', 'LIKE', '%' . $text . '%')
->orWhere('customer.last_name', 'LIKE', '%' . $text . '%')
->orWhere('customer.address', 'LIKE', '%' . $text . '%')
->orWhere('job.name', 'LIKE', '%' . $text . '%')
->orWhere('job.address', 'LIKE', '%' . $text . '%')
->get();
潇潇雨雨
TA贡献1833条经验 获得超4个赞
如果您有多个 where 条件,那么您可以像这样使用 where 函数和 orWhere:
\DB::table('id')->join('customer','customer.customer_id','id.customer_id')->join('job','job.id','id.job_id')->where(function ($query) use($text) { $query->where('customer.first_name', 'LIKE', '%' . $text .'%') ->orWhere('customer.last_name', 'LIKE', '%' . $text . '%') ->orWhere('customer.address', 'LIKE', '%' . $text . '%') ->orWhere('job.name', 'LIKE', '%' . $text . '%') ->orWhere('job.address', 'LIKE', '%' . $text . '%') })->get();
- 2 回答
- 0 关注
- 141 浏览
添加回答
举报
0/150
提交
取消