0

How can I use the if statement in MYSQL query to return yes if it's true and no if it's not?

See the example :

IF(table_name.field_name_value = 1, 'yes','no') AS active

I'm trying something like this but with no success

6
  • What do you mean by " with no success "? You wanted to say that " this is not working "? Commented Nov 13, 2011 at 13:33
  • Tell us how it works and what is the expected result, if you want your problem to be solved for you, please. Commented Nov 13, 2011 at 13:45
  • @Tadeck, when i print active it doesn't show anything Commented Nov 13, 2011 at 13:49
  • @Tadeck,print_r($array['active']); Commented Nov 13, 2011 at 13:53
  • Your problem may be more complex and may not be related to this expression. The query works - see my updated answer. Commented Nov 13, 2011 at 13:57

4 Answers 4

1

From the documentation on IF() function:

mysql> SELECT IF(1<2,'yes','no');
        -> 'yes'

So, basically, the syntax is as mentioned above.

EDIT:

You are saying my example is incorrect. Thus, I provide you a proof (which proves the example from the documentation I mentioned above):

I have a table, which has uid column, containing ID of the user. The table is part of the database on some site (which is not relevant). When I make the following query:

SELECT `uid`, IF(`uid`=3, 'yes', 'no') AS `active` FROM `mysite_users`;

I receive the following result:

+-----+--------+
| uid | active |
+-----+--------+
|   0 | no     |
|   1 | no     |
|   3 | yes    |
|   8 | no     |
|   9 | no     |
|  10 | no     |
|  11 | no     |
|  12 | no     |
|  13 | no     |
|  14 | no     |
|  15 | no     |
+-----+--------+
11 rows in set (0,00 sec)

Which is exactly what I would expect (and what should be expected after reading the documentation). Is it still not working for you?

Sign up to request clarification or add additional context in comments.

4 Comments

i'm trying to compare a field value with a static number, as i said above. my example is not correct so yours either!
@Revo: It is not my example, it is an example from the documentation (see page linked within my answer), so how could it be incorrect? Have you even checked it?
the problem is using AS !
@Revo: See the updated answer. This is certainly working - I have tested it twice and it works exactly at the documentation says.
1

You can use case function:

select case
   when table_name.field_name_value = 1 then 'yes'
   else 'no'
   end case as active
  from table_name

Although if function should also work. Can you post your full query?

Comments

1

You can use CASE in mysql query for that. Like

CASE WHEN table_name.field_name_valud = 1 THEN 'Yes' ELSE 'No' END As active

Comments

1

See http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

SELECT CASE  WHEN table_name.field_name_value = 1 THEN 'yes'
   ELSE 'no' END;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.