1

I need to run a query dependent on the value of a field read by that specific query. This is my query :

SELECT * FROM foo JOIN bar ON bar.ID = foo.field1

if foo.field1 is 0, the JOIN is to be made on foo.field2

I've tried something like

SELECT * FRMO foo JOIN bar ON bar.ID = (SELECT field1 FROM bar WHERE field1 != 0)

but I'm not sure how how to write the next part, for when field1 is actually 0.

Please advise. Thanks!

1 Answer 1

1

Use a CASE statement:

SELECT *
FROM foo
JOIN bar 
    ON bar.ID = CASE foo.field1 WHEN 0 THEN foo.field2 ELSE foo.field1 END
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.