1

I have an table A contain account_id and type

If type = 1 -> account_id will join with table User
If type = 2 -> account_id wwill join with table Customer

I have no idea how to write an SQL query?

Please help

1
  • Why not use PHP to decide? Commented Nov 18, 2017 at 16:41

1 Answer 1

1

In SQL, you would write this with two left joins:

select a.*,
       coalesce(u.name, c.name) as name  -- how you access a field
from a left join
     users u
     on a.account_id = u.user_id and a.type = 1 left join
     customers c
     on a.account_id = c.customer_id and a.type = 2;
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.