1. 程式人生 > 資料庫 >postgresql 實現查詢出的資料為空,則設為0的操作

postgresql 實現查詢出的資料為空,則設為0的操作

我就廢話不多說了~

select name,price from odoo_sale;

postgresql 實現查詢出的資料為空,則設為0的操作

select name,COALESCE(price,0) from odoo_sale;

postgresql 實現查詢出的資料為空,則設為0的操作

補充:postgresql查詢某列的最大值時,對查詢結果為空做預設為0的處理

例項如下:

select coalesce(max(max_grade),0)
    from exam_grade
    where examinee_id = 12345
    and exam_id = 1;

查詢某個考生在某個指定試卷的最高分,如果沒有,則返回0

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。