Welcome to WebmasterWorld Guest from 54.196.175.173

Forum Moderators: open

Message Too Old, No Replies

MYSQL IF statement,

if value= this, show this

     

Anyango

8:26 am on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If there is a field named "status", it can either have 0 or 1 as value. when we select the field, is there any way where we can display "Accepted" for a 1, and "Rejected" for a 0 ?

Something like

IF status=0 Display "Rejected"
IF status=1 Display "Accepted"

in a simple select query ?

phranque

3:20 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



you could probably do something using a temporary table...

Frank_Rizzo

4:56 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Phranque - either I or you have read the question wrong. Surely this is a simple if() ?

SELECT IF(STATUS = 0, 'Rejected', 'Accepted') from TABLE

If the status field had another value such as 2 for "On Hold" then you can just nest the ifs.

SELECT IF(STATUS = 0, 'Rejected', IF(STATUS = 1, 'Accepted', 'On Hold')) from TABLE

rocknbil

7:21 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Nested example . . .

select username,firstname,lastname,if(status = 0, 'Rejected', 'Accepted') from table;

Documentation [dev.mysql.com]

Anyango

6:11 am on Mar 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thats cool, Thanks everyone
 

Featured Threads

Hot Threads This Week

Hot Threads This Month