Arunkumar Thangavel
Arunkumar Thangavel

Reputation: 115

select all or only specific rows from a table

Is it possible to fetch records based on where condition and if the condition is not satisfied , then all the records from the table must be displayed.

for example, i have customer ids 1,2,3,4. if i give 1 as c_id in where condition, it must display that particular record. if i give 5 as c_id, it must display all the records from the table. is it possible to achieve in a single query?

Below is the query which i have tried.

SELECT case
 WHEN c_id in ('6') then 1          
      else 0
       END as y from customer

Upvotes: 1

Views: 131

Answers (1)

jakubiszon
jakubiszon

Reputation: 3573

You can try something like this:

select *
from customer
where c_id = 6

union all

select *
from customer
where not exists (
    select null
    from customer
    where c_id = 6
)

Upvotes: 1

Related Questions