如何过滤 RETURNING *?
How can I filter RETURNING *?
我有以下场景。我有一个 table,它有一个 IsDeleted 标志,我设置它用于记录 'soft delete'。我正在做一个 UPSERT,我在其中添加、修改和标记为删除了一些记录。我想从 RETURNING 语句中排除已标记为已删除的记录。我试图将 WHERE tbltest_IsDeleted = 0
附加到以下 SQL 的末尾,但它给了我错误: ERROR: syntax error at or near "WHERE"
如何过滤以下语句中RETURNING *
的结果?
INSERT INTO tbltest (
tbltest_ID,
tbltest_Name,
tbltest_Description,
tbltest_IsDeleted)
VALUES
(DEFAULT, 'new record','new record description', 0),
(4, 'modified record name','modified record description', 0),
(5, 'existing record name','existing record description', 1)
ON CONFLICT (tbltest_ID) DO UPDATE SET (
tbltest_Name,
tbltest_Description,
tbltest_IsDeleted) = (
excluded.tbltest_Name,
excluded.tbltest_Description,
excluded.tbltest_IsDeleted) RETURNING *;
解决了,这是我能够做到的:
WITH rows AS (
INSERT INTO tbltest (
tbltest_ID,
tbltest_Name,
tbltest_Description,
tbltest_IsDeleted)
VALUES
(DEFAULT, 'new record','new record description', 0),
(4, 'modified record name','modified record description', 0),
(5, 'existing record name','existing record description', 1)
ON CONFLICT (tbltest_ID) DO UPDATE SET (
tbltest_Name,
tbltest_Description,
tbltest_IsDeleted) = (
excluded.tbltest_Name,
excluded.tbltest_Description,
excluded.tbltest_IsDeleted) RETURNING *
)
SELECT * FROM rows WHERE rows.tbltest_IsDeleted = 0
希望这可以节省一些时间 ;-)
我有以下场景。我有一个 table,它有一个 IsDeleted 标志,我设置它用于记录 'soft delete'。我正在做一个 UPSERT,我在其中添加、修改和标记为删除了一些记录。我想从 RETURNING 语句中排除已标记为已删除的记录。我试图将 WHERE tbltest_IsDeleted = 0
附加到以下 SQL 的末尾,但它给了我错误: ERROR: syntax error at or near "WHERE"
如何过滤以下语句中RETURNING *
的结果?
INSERT INTO tbltest (
tbltest_ID,
tbltest_Name,
tbltest_Description,
tbltest_IsDeleted)
VALUES
(DEFAULT, 'new record','new record description', 0),
(4, 'modified record name','modified record description', 0),
(5, 'existing record name','existing record description', 1)
ON CONFLICT (tbltest_ID) DO UPDATE SET (
tbltest_Name,
tbltest_Description,
tbltest_IsDeleted) = (
excluded.tbltest_Name,
excluded.tbltest_Description,
excluded.tbltest_IsDeleted) RETURNING *;
解决了,这是我能够做到的:
WITH rows AS (
INSERT INTO tbltest (
tbltest_ID,
tbltest_Name,
tbltest_Description,
tbltest_IsDeleted)
VALUES
(DEFAULT, 'new record','new record description', 0),
(4, 'modified record name','modified record description', 0),
(5, 'existing record name','existing record description', 1)
ON CONFLICT (tbltest_ID) DO UPDATE SET (
tbltest_Name,
tbltest_Description,
tbltest_IsDeleted) = (
excluded.tbltest_Name,
excluded.tbltest_Description,
excluded.tbltest_IsDeleted) RETURNING *
)
SELECT * FROM rows WHERE rows.tbltest_IsDeleted = 0
希望这可以节省一些时间 ;-)