0

I want to select multiple column values based on multiple where clauses in SQL. I am not able to do it.

I am trying something like,

select c.fname,r.fname from customer as c LEFT JOIN retailer as r ON
r.customer_id = c.id where c.id > 10 OR r.id < 50.

Basically I want both my where clauses to get executed one by one and first output(when 'where c.id>10' is executed) should come as c.fname and second output (when 'where r.id<50') in one sql query.

Can it be done ? Please help.

1
  • I can not understand, you want all this in same result ? For both where clause ? Commented Dec 22, 2015 at 11:19

2 Answers 2

0

I think you need a union, not a join:

select c.fname, 'customer' as type from customer as c where c.id > 10
union
select r.fname, 'retailer' from retailer as r where r.id < 50

This would give you the list from both tables, independently from each other, within 1 query.

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

Comments

0

Try the following query:

SELECT
c.fname,
r.fname
CASE WHEN r.id < 50 THEN 'output 1'             
     WHEN c.id > 10 THEN 'output 2' END AS 'OUTPUT_COLUMN'
FROM customer as c 
LEFT JOIN retailer as r 
ON r.customer_id = c.id

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.