CREATE TABLE users (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
UNIQUE(username)
);
CREATE TABLE alert_triggers(
id SERIAL PRIMARY KEY,
uid INTEGER REFERENCES users(id),
price_hi REAL,
active_hi BOOLEAN DEFAULT TRUE,
price_lo REAL,
active_lo BOOLEAN DEFAULT TRUE
);
CREATE TABLE alerts (
id SERIAL PRIMARY KEY,
uid INTEGER REFERENCES users(id),
event INTEGER NOT NULL
);
CREATE TABLE prices (
stamp DATETIME PRIMARY KEY,
price REAL
);
在插入新价格后,我要执行以下四个操作:
为此,我需要同时更新和查询一个表并将查询结果插入到另一个表中。
事件:
Price Above PRICE_HI 1
Price Below PRICE_LO 2
这就是我被困住的地方。我想执行以下操作:
INSERT INTO alerts (
uid,
event
) SELECT uid,1
FROM (
UPDATE alert_triggers
SET active_hi = FALSE
WHERE active_hi = TRUE AND price_hi <= 5.0
RETURNING uid
);