4
我有兩個數據庫表:在PL/pgSQL中,是否可以將字符串作爲表達式來計算,而不是語句?
# \d table_1
Table "public.table_1"
Column | Type | Modifiers
------------+---------+-----------
id | integer |
value | integer |
date_one | date |
date_two | date |
date_three | date |
# \d table_2
Table "public.table_2"
Column | Type | Modifiers
------------+---------+-----------
id | integer |
table_1_id | integer |
selector | text |
table_2.selector
中的值可以是一個one
,two
,或three
,以及用於選擇在table_1
日期列之一。
我的第一個執行使用CASE
:
SELECT value
FROM table_1
INNER JOIN table_2 ON table_2.table_1_id = table_1.id
WHERE CASE table_2.selector
WHEN 'one' THEN
table_1.date_one
WHEN 'two' THEN
table_1.date_two
WHEN 'three' THEN
table_1.date_three
ELSE
table_1.date_one
END BETWEEN ? AND ?
的值selector
是這樣的,我可以找出感興趣的列eval(date_#{table_2.selector})
,如果 PL/pgSQL的允許作爲字符串表達式的評價。
我能找到的最接近的是EXECUTE string
,它評估整個報表。有什麼方法可以評估表達式?
你所要求的是不可能的,就像[@klin解釋](http://stackoverflow.com/a/31361452/939860)。如果您試圖使查詢更快*,有幾個選項。如果是這樣,請提供您的Postgres版本,基數,*所有*約束(PK,UNIQUE,FK,...)並回答這個問題:'t2中是否有更多列或是全部? –