3

我有这个查询:

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 放在括号内,但没有成功。

我应该如何使用别名?

4

3 回答 3

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)forvariationid

于 2013-09-13T11:27:11.390 回答
1

一种解决方法是将 更改values为 a 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一般。

您也许可以只删除语句中的as cidif Postgres 支持子查询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;
于 2013-09-13T11:28:53.497 回答
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
于 2013-09-13T12:50:49.653 回答