Kailash PK
Kailash PK

Reputation: 71

How to add if else or case in SQL select statement

This is my SQL query to fetch data from the tables order and user_master

String str = "select od.total, od.invoice_link, od.status, ";
str += "od.order_type, um.name as sender_name, umr.name as receiver_name, ";
str += "umr.twitter_handle as receiver_twitter_handle, um.twitter_handle as sender_twitter_handle ";
str += "from orders od ";
str += "join user_master um on um.id = od.user_id ";
str += "join user_master umr on umr.id = od.receiver_id ";
str += "where user_id = '"+user_id+"' ";

String json = dao.getResultJSON(str);
return json;

In the database there is this column named order_type which contains values like transfer, request and invoice.I need to return "Payment to" into the variable json if the value in the order_type column is 'transfer' and 'Requested from' if the value in the column is 'request'.Just forget about 'invoice' which i mentioned above.These two will be enough for my expected output..

Anyway thanks in advance

Upvotes: 0

Views: 108

Answers (2)

O. Jones
O. Jones

Reputation: 108776

select od.total, 
       od.invoice_link, od.status,
       od.order_type,
       um.name as sender_name, umr.name as receiver_name,
       umr.twitter_handle as receiver_twitter_handle, 
       um.twitter_handle as sender_twitter_handle 
  from orders od 
  join user_master um on um.id = od.user_id 
  join user_master umr on umr.id = od.receiver_id 
  where user_id = SOMETHING 

is your SQL query dissected out from what appears to be Java. You should understand that SQL deals in tables; that is, it deals in rectangles full of data. Each result set returns a series of rows. Each row in a result set necessarily must have the same columns as the other rows.

You are asking for two different kinds of rows, one with a Payment to column and the other with a Requested from column. The choice of row depends on some data in the row. This is not going to happen in a single query.

You can do something like this, though.

select 
 ...,
 CASE WHEN od.order_type='transfer' THEN receiver_twitter_handle ELSE '' END `Payment to`,
 CASE WHEN od.order_type='request' THEN sender_twitter_handle ELSE '' END `Requested from`,
 ...

This will yield the two columns you want in every row of your result set. One, or both, the columns will contain blanks when irrelevant.

The details of these CASE statements can vary among makes of table server, and you haven't disclosed which one you're using, so there may be a mistake here. But you can fix it.

Upvotes: 0

SqlZim
SqlZim

Reputation: 38043

Without knowing where payment_to and requested_from are supposed to come from, here is as shot in the dark:

select 
    od.total
  , od.invoice_link
  , od.status
  , od.order_type
  , um.name as sender_name
  , umr.name as receiver_name
  , um.twitter_handle as sender_twitter_handle 
  , umr.twitter_handle as receiver_twitter_handle
  , case when od.order_type = 'transfer' then od.payment_to else null end as payment_to 
  , case when od.order_type = 'request' then od.requested_from else null end as requested_from
  from orders od 
    inner join user_master um on um.id = od.user_id 
    inner join user_master umr on umr.id = od.receiver_id 
  where user_id = '"+user_id+"' 

In the format you posted:

String str = "select";
str += "    od.total";
str += "  , od.invoice_link";
str += "  , od.status";
str += "  , od.order_type";
str += "  , um.name as sender_name";
str += "  , umr.name as receiver_name";
str += "  , um.twitter_handle as sender_twitter_handle ";
str += "  , umr.twitter_handle as receiver_twitter_handle";
str += "  , case when od.order_type = 'transfer' then od.payment_to else null end as payment_to";
str += "  , case when od.order_type = 'request' then od.requested_from else null end as requested_from";
str += "  from orders od ";
str += "    inner join user_master um on um.id = od.user_id ";
str += "    inner join user_master umr on umr.id = od.receiver_id ";
str += "  where user_id = '"+user_id+"';";

String json = dao.getResultJSON(str);
return json;

Upvotes: 1

Related Questions