Welcome to WebmasterWorld Guest from 54.166.224.46

Forum Moderators: open

Message Too Old, No Replies

MYSQL IF statement,

if value= this, show this

     
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 ?
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...
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
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]
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