簡體   English   中英

如何從表中選擇除外表中的值以外的所有值?

[英]How to select all the values from a table except the values which are in foreign table?

我有三個表: tbl_borrower, tbl_clienttbl_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM