我正在构建查询,显然,它不起作用。这是我当前的查询(这是完整的查询)
IF NOT EXISTS(SELECT * FROM invite WHERE uid=$1::uuid)
BEGIN
INSERT INTO invite (uid, link) VALUES ($1::uuid, $2::text);
END
ELSE
BEGIN
SELECT * FROM invite WHERE uid=$1::uuid;
END
在我的控制台中,我收到syntax error at or near "IF"
答案 0 :(得分:0)
代码中的许多错误:
纯SQL可能更好:
WITH q AS (
SELECT *
FROM invite
WHERE uid=$1::uuid
), i AS (
INSERT INTO invite (uid, link)
SELECT $1::uuid, $2::text
WHERE (SELECT count(*) = 0 FROM q)
RETURNING *
) SELECT * FROM q
UNION SELECT * FROM i