0

How can I return d.title or u.name in the outer SELECT clause?

SELECT c.id, c.name
FROM components c 
INNER JOIN publications p 
ON c.id = p.component_id
AND p.document_id IN 
    (SELECT d.id FROM documents d WHERE user_id IN 
        (SELECT u.id FROM users u WHERE u.brand_id IN (39, 41)
    )
)

I get this error when I throw d.title in the top line:

missing FROM-clause entry for table "d" LINE 1

The package I'm using needs these values returned on the top line to make any use out of them in the result.

Structure

A User has many Documents, and Publications is the join table between Documents and Components.

2
  • 3
    It would be easier to give a definitive answer if you showed the structure of the tables, but in general, using joins instead of IN clauses will let you refer to the tables to which you are applying criteria. Commented Jul 6, 2017 at 18:47
  • As @rd_nielsen said, you will need to join in these tables (p.document_id = documents.id and documents.user_id = users.id) if you want to reference them in the select statement. Commented Jul 6, 2017 at 18:50

1 Answer 1

2

Use below query -

SELECT c.id, c.name, d.title, u.name
FROM components c 
INNER JOIN publications p ON c.id = p.component_id
INNER JOIN documents d ON d.id = p.document_id
INNER JOIN users u ON d.user_id = u.id
AND u.brand_id IN (39, 41)

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

That helps so much. Cheers!

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.