1

I have the following setup:

CREATE TABLE A(id INT);
CREATE TABLE B(id INT);
CREATE TABLE C(aid INT, bid INT);

I would like to do the following:

INSERT INTO C SELECT A.id as aid, (SELECT B.id FROM B WHERE A.id=B.id) 
AS bid FROM A WHERE bid is not NULL;

However it gives an error that the column bid doesn't exist. So is there a way to refer to an output row in lateral join or make insert only in case bid is not null?

I would like to keep the lateral join though.

1 Answer 1

2

You can't reference the alias like that, you need to wrap the select:

INSERT INTO C 
SELECT *
FROM (
    SELECT A.id as aid, 
           (SELECT B.id FROM B WHERE A.id=B.id) AS bid 
    FROM A 
) t
WHERE bid is not NULL;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.