homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

MYSQL IF statement,
if value= this, show this

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

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)

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


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

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)

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)

Thats cool, Thanks everyone

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved