将mysql查询转换为postgresql

时间:2011-07-25 15:26:23

标签: mysql postgresql

这个查询在mysql中工作得很好,但是我试图在postgresql中运行它并且它抱怨IGNORE关键字,这不是一个pg关键字。我不确定如何修改它以使其在pg下执行相同的操作?

DROP TABLE IF EXISTS ct_tmp_u1101_t9;
CREATE TEMPORARY TABLE ct_tmp_u1101_t9(id int primary key, shared int default 0) IGNORE (SELECT 1101 as id);
SELECT shared from ct_tmp_u1101_t9
UNION (SELECT cust_user2role.userid AS userid FROM cust_user2role
INNER JOIN cust_users ON cust_users.id = cust_user2role.userid
INNER JOIN cust_role ON cust_role.roleid = cust_user2role.roleid 
WHERE cust_role.parentrole like 'H1::H2::H3::H4::H5::%')
UNION (SELECT groupid FROM cust_groups where groupid in (3,4,2,1005));

1 个答案:

答案 0 :(得分:1)

除非我遗漏了一些绝对明显的东西,否则我认为不需要临时表。

以下内容应返回完全相同的结果(除非临时表中存在一些MySQL魔法)

SELECT 0 as shared
UNION 
SELECT cust_user2role.userid AS userid 
FROM cust_user2role
   INNER JOIN cust_users ON cust_users.id = cust_user2role.userid
   INNER JOIN cust_role ON cust_role.roleid = cust_user2role.roleid 
   WHERE cust_role.parentrole like 'H1::H2::H3::H4::H5::%'
UNION 
SELECT groupid 
FROM cust_groups 
WHERE groupid IN (3,4,2,1005);

(我认为同样适用于MySQL)