Skip to content
Advertisement

How to write query for this?

Hi friends I have a table like this

ID  bid   sub_bid  cid  sub_cid
1    0       2      1       0
2    5       0      3       0
3    3       0      0       4
4    2       0      4       0

on that table either (bid or sub_bid) OR (cid or sub_cid) will be null. I have to write a query for fetching like this.. if bid is zero then I have to take sub_bid or sub_bid is zero then I have to take bid same incase of cid also.

How can I give this kind of a condition in my mysql query. any one can help me please. thanks

Advertisement

Answer

Have a look at the CASE WHEN statement:

   SELECT
    CASE WHEN bid is NULL or bid = 0
     sub_bid 
    ELSE 
     bid end 
    as abid

http://dev.mysql.com/doc/refman/5.0/en/case-statement.html http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement