learner
learner

Reputation: 2129

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

Upvotes: 0

Views: 77

Answers (2)

Kevin Burton
Kevin Burton

Reputation: 11936

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

Upvotes: 3

Balanivash
Balanivash

Reputation: 6867

You can use the MySQL if statements for this. Check this

Upvotes: 1

Related Questions