更新时间:2023-09-14 21:19:04
您将在postgres中这样编写。 IGNORE
在这里无关紧要,因为该表刚刚被重新创建并且保证为空。并且 UNION
保证没有重复行插入。
You would write like this in postgres.IGNORE
is irrelevant here, as the table has just been recreated and is guaranteed to be empty. And UNION
guarantees there are no duplicate rows inserted.
DROP TABLE IF EXISTS tmp_table;
CREATE TEMP TABLE tmp_table(id int4 primary key);
INSERT INTO tmp_table
SELECT user2role.userid::int4 AS id
FROM user2role
JOIN users ON users.id = user2role.userid
JOIN role ON role.roleid = user2role.roleid
WHERE role.parentrole like 'H1::H2::H3::H4::H5::%'
UNION
SELECT groupid::int4
FROM groups
WHERE groupid in (2,3,4);
如果SELECT中不能出现重复,则可以考虑使用 UNION ALL更快
而不是 UNION
。否则,您需要 UNION
来消除可能的欺骗。在此处中阅读。
如果数据集很大,则可以考虑在INSERT之后 创建主键。这样更快。
If duplicates in the SELECT cannot occur, you might consider the faster UNION ALL
instead of UNION
. Otherwise you need UNION
to eliminate possible dupes. Read here.
If your dataset is large you might consider creating the primary key after the INSERT. That's faster.
阅读 mySQL文档对 IGNORE
的影响。
在重新访问页面时,我意识到您在原始代码中提到了 IF NOT EXISTS
。
您不是这样说的,但这仅在原始代码仅在表不存在的情况下创建了表的情况下才有意义,这可能导致表在不为空之前插。在这种情况下, IGNORE
是相关的,并且需要与PostgreSQL中的等效项。
On revisiting the page I realized you mention IF NOT EXISTS
in the original code.
You don't say so, but that only makes sense if the original code created the table only if it didn't exist already, which introduces the possibility of it being not empty before the INSERT. In this case IGNORE
is relevant and needs an equivalent in PostgreSQL.
所以这是 替代答案 来解释您的问题。
So here is alternative answer for that interpretation of your question.
如果不存在则创建温度表
已在PostgreSQL 9.1 中实现。
对于较旧的版本,我发布了解决方案。
CREATE TEMP TABLE IF NOT EXISTS tmp_table(id int4 primary key);
INSERT INTO tmp_table
SELECT x.id
FROM (
SELECT user2role.userid::int4 AS id
FROM user2role
JOIN users ON users.id = user2role.userid
JOIN role ON role.roleid = user2role.roleid
WHERE role.parentrole like 'H1::H2::H3::H4::H5::%'
UNION
SELECT groupid::int4
FROM groups
WHERE groupid in (2,3,4)
) x
LEFT JOIN tmp_table t USING (id)
WHERE t.id IS NULL;
左联接... t.id为空$ c>排除
tmp_table
中可能已经存在的任何 id
。 UNION
进入子选择,因此该子句只需要应用一次。应该是最快的。
在此处详细了解LEFT JOIN 。
LEFT JOIN ... WHERE t.id IS NULL
excludes any id
that might already be present in tmp_table
. UNION
goes into a sub-select, so that clause needs only be applied once. Should be fastest.
More on LEFT JOIN here.