我創建了PostgreSQL的這些表基於這個話題:重寫選擇SQL查詢PostgreSQL的
Database design for user settings
-- TABLE SETTING
CREATE TABLE SETTING(
ID INTEGER NOT NULL,
DESCRIPTION TEXT,
CONSTRAINED TEXT,
DATA_TYPE TEXT,
MIN_VALUE TEXT,
MAX_VALUE TEXT
)
;
-- ADD KEYS FOR TABLE SETTING
ALTER TABLE SETTING ADD CONSTRAINT KEY34 PRIMARY KEY (ID)
;
-- TABLE ALLOWED_SETTING_VALUE
CREATE TABLE ALLOWED_SETTING_VALUE(
ID INTEGER NOT NULL,
SETTING_ID INTEGER,
ITEM_VALUE TEXT,
CAPTION TEXT
)
;
-- CREATE INDEXES FOR TABLE ALLOWED_SETTING_VALUE
CREATE INDEX IX_RELATIONSHIP16 ON ALLOWED_SETTING_VALUE (SETTING_ID)
;
-- ADD KEYS FOR TABLE ALLOWED_SETTING_VALUE
ALTER TABLE ALLOWED_SETTING_VALUE ADD CONSTRAINT KEY35 PRIMARY KEY (ID)
;
-- TABLE USER_SETTING
CREATE TABLE USER_SETTING(
ID INTEGER NOT NULL,
USER_ID INTEGER,
SETTING_ID INTEGER,
ALLOWED_SETTING_VALUE_ID INTEGER,
UNCONSTRAINED_VALUE TEXT
)
;
-- CREATE INDEXES FOR TABLE USER_SETTING
CREATE INDEX IX_RELATIONSHIP15 ON USER_SETTING (SETTING_ID)
;
CREATE INDEX IX_RELATIONSHIP17 ON USER_SETTING (ALLOWED_SETTING_VALUE_ID)
;
-- ADD KEYS FOR TABLE USER_SETTING
ALTER TABLE USER_SETTING ADD CONSTRAINT KEY36 PRIMARY KEY (ID)
;
但是當我運行選擇SQL查詢,我得到的錯誤,因爲它是爲MySQL:
-- Show settings for a given user
select
US.user_id
, S1.description
, S1.data_type
, case when S1.constrained = 'true'
then AV.item_value
else US.unconstrained_value
end value
, AV.caption
from USER_SETTING US
inner join SETTING S1
on US.setting_id = S1.id
left outer join ALLOWED_SETTING_VALUE AV
on US.allowed_setting_value_id = AV.id
where US.user_id = 234
結果
ERROR: syntax error at or near "value"
LINE 8: end value
Howe我可以重寫這個SQL查詢PostgreSQL?
這是有效的標準SQL,嘗試添加'AS':'AS值'(我剛剛在小提琴上試過,這工作)。似乎是一個奇怪的解析錯誤。 – dnoeth
是的,這解決了這個問題。謝謝! –