環境: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 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消