我正在更改一個 db 函式,我需要添加一個查詢,以查找與其類別 ID 匹配的專案與我給出的陣列中的任何專案。
示例輸入:
let categories = ['10272111', '165796011', '3760911', '9223372036854776000','3760901','7141123011']
部分資料庫功能:
(case when brand is null then '' else 'AND ra.brand ILIKE ''%%'||brand||'%%''' end),
(case when category is null then '' else 'AND ra.root_category IN ('||category||')' end),
(case when asin is null then '' else 'AND ra.asin ILIKE ''%%'||asin||'%%''' end),
但我不斷收到malformed array literal: "AND ra.root_category IN ("錯誤,我找不到正確語法的任何資源。
編輯:這是函式頂部的定義:
category text[] DEFAULT NULL::text[],
uj5u.com熱心網友回復:
作為一種通用方法,使用= any和 一串以逗號分隔的值,而不是IN.
首先更改函式宣告,更改category text[]為category text.
然后改變
(case when category is null then '' else 'AND ra.root_category IN ('||category||')' end)
至
(case when category is null then '' else 'AND ra.root_category = any(''{'||category||'}'')' end)
最后在 JS 中將類別陣列轉換為字串,即.join(',')類別陣列并將結果字串傳遞給函式。
不相關,但據我所知,不需要動態 SQL。嘗試
AND (category is null OR ra.root_category = any('{'||category||'}'))
-- or the equivalent
AND (category is null OR ra.root_category = any(string_to_array(category, ',')))
uj5u.com熱心網友回復:
我做的!
函式宣告:category text DEFAULT NULL::text
(case when category is null then '' else 'AND ra.root_category IN '||category end),
是查詢,我以這種方式發送陣列:
let categories = "(" id_list.map((id) => "'" id"'") ")";
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/516617.html
