Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error for insert query alias in Postgres

I have this query:

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

and I'm getting this error:Query failed: ERROR: syntax error at or near "AS"

I've tried putting cid inside the bracket, without success.

How should I use the alias?

like image 930
Alexandru R Avatar asked Feb 10 '26 15:02

Alexandru R


1 Answers

Try to change to select instead of values as below

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)

I think you should replace cit with (SELECT id FROM campaigns WHERE mongoid=$4) for variationid column

like image 63
Robert Avatar answered Feb 13 '26 09:02

Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!