tags:

views:

81

answers:

3

Hi guys could you please help me out here.

SELECT e.name, IF e.active = 0 THEN e.active = true WHERE e.id = #{estate_id} ELSE e.active = false WHERE e.id = #{estate_id} END IF AS Estate_status FROM estates e

+2  A: 

This is db2 syntax as you have not mentioned which DB you are using:

select e.name,
    case when e.active = 0 then 'TRUE'
        else 'FALSE'
    end AS Estate_status,
    from estates e
where e.id = #{estate_id}
Rashmi Pandit
Thanks it worked like a charm, by the way i am using mysql..
Donald
It worked with the same syntax or did you have to edit it?
Rashmi Pandit
I am glad it worked :)
Rashmi Pandit
A: 

I'm confused by the rest of your SQL statement. What exactly are you trying to achieve.

However, in regard to the question itself, you can't use an IF statement within the SQL like that. You can however use a CASE statment, such as:

CASE
 WHEN e.active = 0 THEN 'true'
 ELSE 'false'
END
Robin Day
+1  A: 

use case statement : SELECT e.name, (case when e.active = 0 THEN e.active = 1 ELSE e.active = 0 END) AS Estate_status FROM estates e WHERE e.id = #{estate_id}