[英]Subtract 2 Columns from different tables
我正在嘗試建立一個查詢,其中減去了來自不同表的2列。 這是我嘗試的:
DB::connection('lab_inv')->where('tab2'.'Amount_Run', '=', 'tab1'.'Amount')->selectraw('tab1.Amount - tab2.Amount_Run');
第一個表的值“ Amount”對於每個ID都是唯一的,第二個表由外鍵綁定到第一個表的ID,並且參數“ Amount_Run”也隨ID的不同而不同。 金額=金額–金額運行。 任何想法我在做什么錯。
如果我正確理解了您的問題,那么簡單的聯接然后減去就可以解決問題:
DB::connection('lab_inv')
->table('tab1')
->join('tab2', 'tab1.id', 'tab2.tab1_id') // replace this with the actual foreign key
->selectraw('tab1.Amount - tab2.Amount_Run AS amount')
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.