Postgres中插入查询别名的语法错误

时间:2013-09-13 11:24:31

标签: mysql sql postgresql

我有这个问题:

INSERT INTO emailevents (shopid, userid, emailid, campaignid, variationid, type, createdon) 
VALUES ($1,                 
(SELECT id FROM users WHERE mongoid=$2), 
(SELECT id FROM emails WHERE mongoid=$3), 
(SELECT id FROM campaigns WHERE mongoid=$4) AS cid, 
(SELECT id FROM campaignvariations WHERE templateid=(SELECT id FROM templates WHERE mongoid=$5) AND campaignid=cid), 
$6, 
to_timestamp($7))

我收到此错误:Query failed: ERROR: syntax error at or near "AS"

我已经尝试将cid放在括号内,但没有成功。

我应该如何使用别名?

3 个答案:

答案 0 :(得分:2)

尝试更改为select而不是values,如下所示

INSERT INTO emailevents (shopid, userid, emailid, campaignid, variationid, type, createdon)      
select $1,                 
       (SELECT id FROM users WHERE mongoid=$2), 
       (SELECT id FROM emails WHERE mongoid=$3), 
       (SELECT id FROM campaigns WHERE mongoid=$4) AS cid, 
       (SELECT id FROM campaignvariations WHERE templateid=(SELECT id FROM templates WHERE mongoid=$5) AND campaignid=(SELECT id FROM campaigns WHERE mongoid=$4)), 
       $6, 
       to_timestamp($7)

我认为您应该将cit替换为(SELECT id FROM campaigns WHERE mongoid=$4)列的variationid

答案 1 :(得分:1)

一种解决方法是将values更改为select

INSERT INTO emailevents (shopid, userid, emailid, campaignid, variationid, type, createdon) 
    select $1,                 
           (SELECT id FROM users WHERE mongoid=$2), 
           (SELECT id FROM emails WHERE mongoid=$3), 
           (SELECT id FROM campaigns WHERE mongoid=$4) AS cid, 
           (SELECT id FROM campaignvariations WHERE templateid=(SELECT id FROM templates WHERE mongoid=$5) AND campaignid=cid), 
            $6, 
            to_timestamp($7);

我更喜欢使用insert . . . select而不是insert . . . values

如果Postgres支持as cid语句中的子查询,您可以删除values

编辑:

以上修复了as问题,但不解决整体问题。让我们使用select并将大多数子查询移到from子句:

INSERT INTO emailevents (shopid, userid, emailid, campaignid, variationid, type, createdon) 
    select const.shopid,  u.id, e.id, c.id                
           (SELECT id
            FROM campaignvariations
            WHERE templateid=(SELECT id FROM templates WHERE mongoid=$5) AND campaignid=c.id
           ), 
           const.type, 
           const.createdon
    from (select $1 as shopid, $6 as type, to_timestamp($7) as createdon) const left outer join
         (SELECT id FROM users WHERE mongoid=$2) u cross join
         (SELECT id FROM emails WHERE mongoid=$3) e cross join
         (SELECT id FROM campaigns WHERE mongoid=$4) c;

答案 2 :(得分:0)

如果您真的不想使用变量,并且想在查询中使用cid,可以使用cte预先计算它:

with cte_cid as (
    select (select id from campaigns where mongoid=$4) as cid -- need 2 selects in case you don't have record for mongoid=$4
)
insert into emailevents (shopid, userid, emailid, campaignid, variationid, type, createdon)      
select $1 as shopid,              
   (select t.id from users as t where t.mongoid=$2), 
   (select t.id from emails as t where t.mongoid=$3), 
   cid,
   (select t.id from campaignvariations as t where t.templateid=(select tt.id from templates as tt where tt.mongoid=$5) and t.campaignid=c.cid), 
$6,
   to_timestamp($7)
from cte_cid as c