[英]How to select all the values from a table except the values which are in foreign table?
我有三个表: tbl_borrower, tbl_client
和tbl_guarantor
tbl_Client:
id|name|address|email|
tbl_Guarantor:
id|clientid(fk)|Guaranteed_date|borrower_id(fk from borrower table)
我想检索客户端表的所有值,除了Laravel 5.5
控制器中的保证表中存在的值。
一旦你建立了模型和关系,你应该这样做:
Client::doesntHave('guarantor')->get()
https://laravel.com/docs/5.6/eloquent-relationships#querying-relationship-absence
如果您正在使用查询构建器,那么它将是:
$clients = DB::table('tbl_clients')
->join('tbl_guarantor', 'tbl_clients.id', '=', 'tbl_guarantor.clientid')
->select('tbl_clients.*')
->whereNull('tbl_guarantor.clientid')
->get();
https://laravel.com/docs/5.5/queries
基于此答案,在第二个表id上使用左连接和测试NULL的前提下。 https://stackoverflow.com/a/4076157/3585500
尝试这个 :
DB::table('tbl_Client')
->groupBy('tbl_Client.id')
->leftjoin('tbl_Guarantor', 'tbl_Client.id', '=', 'tbl_Guarantor.clientid')
->get([
'tbl_Client.*'
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.