A while back, I asked about changing a MySQL query into Postgresql. And, I think I got that down, but now, I have this other query which is similar, so I did the same sort of thing, but I am not sure if it's working as intended. The Original MySQL query:
create temporary table IF NOT EXISTS temptable(id int(11) primary key, shared int(1) default 0)
ignore (SELECT 6 as id) UNION (SELECT user2role.userid AS userid FROM user2role
INNER JOIN users ON users.id=user2role.userid
INNER JOIN role ON role.roleid=user2role.roleid
WHERE role.parentrole like 'H1::H2::H3::H4::H5::%')
UNION (SELECT groupid FROM groups where groupid in (3,4,2));
My Postgresql(9.1) version:
Create TEMP TABLE IF NOT EXISTS temptable(id int primary key, shared int default 0);
Insert into temptable SELECT x.id FROM ( SELECT user2role.userid AS id FROM user2role
INNER JOIN users ON users.id=user2role.userid
INNER JOIN role ON role.roleid=user2role.roleid WHERE role.parentrole like 'H1::H2::H3::H4::%'
UNION (SELECT groupid FROM groups where groupid in (2,3,4))) x
LEFT JOIN temptable t USING (id) WHERE t.id IS NULL
Now, I am most worried about the ignore (SELECT 6 as id) UNION
bit. I don't quite understand the point of SELECT 6 as id.
Also, this is irrelevant to this query, but I have another similar query that uses replace
instead of ignore. I understand that replace
is a lot like insert
, just that it replaces old values if they have the same key. I couldn't think of a better way, so I just went ahead and used insert
, but should I implement it in a different way instead?
Thanks for everything, guys.