亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果 laravel 中不存在連接表數據,如何設置默認值?

如果 laravel 中不存在連接表數據,如何設置默認值?

PHP
眼眸繁星 2022-10-28 09:58:33
我有兩張桌子。product和inventory。我想 LEFT JOIN product。inventory還有一個基于inventory.stock的求和操作。我的代碼如下,DB::table('product')    ->leftjoin('inventory','product.id','=','inventory.product_id')    ->select('product.id as id',            'product.name as name',            'product.color as color',            'product.unit_price as unit_price',            DB::raw('SUM(inventory.stock)::integer as available_stock'))     ->groupBy('product.id')     ->get();我的問題是有很多產品在庫存表中沒有行。在那種情況下,available_stock就是給我null。而不是null我想顯示一個默認字符串。像“沒有庫存”或“0”之類的東西。我怎樣才能做到這一點?我正在使用postgresql。
查看完整描述

1 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

這樣,您可以設置可用庫存的默認值。


當庫存不可用時,這將為零。


DB::table('product')

->leftjoin('inventory','product.id','=','inventory.product_id')

->select('product.id as id',

        'product.name as name',

        'product.color as color',

        'product.unit_price as unit_price',

        DB::raw('(case when inventory.product_id is null then 0 else SUM(inventory.stock)::integer end) as available_stock'))

 ->groupBy('product.id')

 ->get();


查看完整回答
反對 回復 2022-10-28
  • 1 回答
  • 0 關注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號