函數傳回的記錄具有串聯的資料列
當使用函數為特定目的擷取資料時,可能會遇到傳回的結果記錄的列連接成單一列。當函數用作另一個查詢中的子查詢時,可能會發生這種情況。
理解函數結果
要分解從函數傳回的行並取得各個列,只需單獨執行函數:
SELECT * FROM account_servicetier_for_day(20424, '2014-08-12');
這將提供具有預期柱狀的輸出
解決原始查詢
要解決原始查詢的問題,根據所使用的PostgreSQL 版本,有幾種方法:
PostgreSQL 9.3 或更高版本
使用JOIN LATERAL 連接子查詢並保留行結構:
SELECT '2014-08-12' AS day, 0 AS inbytes, 0 AS outbytes , a.username, a.accountid, a.userid , f.* -- but avoid duplicate column names! FROM account_tab a , account_servicetier_for_day(a.accountid, '2014-08-12') f -- <-- HERE WHERE a.isdsl = 1 AND a.dslservicetypeid IS NOT NULL AND NOT EXISTS ( SELECT FROM dailyaccounting_tab WHERE day = '2014-08-12' AND accountid = a.accountid ) ORDER BY a.username;
PostgreSQL 9.2或更舊
SELECT '2014-08-12' AS day, 0 AS inbytes, 0 AS outbytes , a.username, a.accountid, a.userid , (a.rec).* -- but be wary of duplicate column names! FROM ( SELECT *, account_servicetier_for_day(a.accountid, '2014-08-12') AS rec FROM account_tab a WHERE a.isdsl = 1 AND a.dslservicetypeid Is Not Null AND NOT EXISTS ( SELECT FROM dailyaccounting_tab WHERE day = '2014-08-12' AND accountid = a.accountid ) ) a ORDER BY a.username;
以上是為什麼我的 PostgreSQL 函數傳回的欄位是串聯的,如何修復它?的詳細內容。更多資訊請關注PHP中文網其他相關文章!