23

This is such a simple question but I haven't found an answer anywhere for 2 hours.

How do you use the MYSQL IF statment. No matter what I put in it doesn't work. In SQL Server this is a 5 second job.

Does it need to be within a procedure?

Can you use a SELECT within an IF statement?

Not really interested in the IF function here.

Thanks for any help.

tura
  • 231
  • 1
  • 2
  • 3
  • 1
    What are you asking? You're not really interested in the IF, but you want to know how you use the IF? If you haven't found anything in 2 hours, did you look here? http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html – Nanne Jan 23 '11 at 16:20
  • 1
    @Nanne - I presume the OP is asking how `IF` can be used in a MySQL script. e.g. `IF NOT EXISTS(...) CREATE TABLE` for example. I remember trying to do that before in MySQL workbench and never figuring it out. – Martin Smith Jan 23 '11 at 16:28
  • @Martin may be. Cause 1st result for `IF` is the reference to the MySQL manual in Google. Better if @tura gives a use-case. – Nishant Jan 23 '11 at 16:32
  • 5
    I was looking at the IF statement as opposed to the IF() function used for flow control. http://dev.mysql.com/doc/refman/5.0/en/if-statement.html However it doesn't mention that it can't be used outside of a procedure body, which I was trying to do for debug purposes. The CASE WHEN procedure will work for my purposes, just annoying that the documentation doesn't mention this fact. Thanks for the quick replies. – tura Jan 23 '11 at 16:41

7 Answers7

17

How to use IF statement in select

select if(status>0, 'active', 'inactive') as status from users

Yes, you can use select within IF

Example:

select if((select count(*) from versions) > (select count(*) from groups), true, false) as value
Srihari Goud
  • 780
  • 9
  • 24
5

Does it need to be within a procedure?

Yes, you do need to be in a procedure to use an if statement.

Can you use a SELECT within an IF statement?

Yes you can:

drop procedure if exists sp;

create procedure sp () begin
    if ((select 1 /*from whatever table would work*/)=1) then
        select 'it works';
    end if;
end;

call sp;

drop procedure sp;
Kevin Panko
  • 7,844
  • 19
  • 46
  • 58
Benoit
  • 601
  • 7
  • 5
5

You can select another field from the same query but using select query might not be supported as query can return multiple values , mysql not supporting row concept in a column.

SELECT IF(`field1`>1,`field2`,`field3`) FROM TABLE1 WHERE 1

here field1,field2,field3 are fields in a same table

Does this help you ?

Harish
  • 2,235
  • 4
  • 21
  • 28
  • 2
    The question asked about the [if statement](http://dev.mysql.com/doc/refman/5.5/en/if.html), not the [IF function](http://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html#function_if) – Rich May 07 '13 at 15:54
3

You hinted at it in your comment, but none of the answers here state it explicitly, so I will:

In MySQL, you cannot use an IF statement outside of the body of a stored procedure. The documentation implies this when it introduces the concept as "the IF statement for stored programs" (my emphasis).

As far as I can see, there is no easy way to control execution flow in statements issued at the command line prompt or via the client (e.g. from PHP), except by querying values, then switching control flow based on those values outside of MySQL (i.e. using PHP's if statement instead of MySQL's). I would love to be corrected on this though :-)


You can simulate this on a case-by-case basis using other constructions. For example:

IF x THEN 
  INSERT INTO mytable (mycol)
  VALUES ('foo')
END IF

could become

INSERT INTO mytable (mycol)
SELECT 'foo'
FROM dual
WHERE x

(as per this answer)


You can also create, call and then drop a new stored procedure:

DELIMITER \\
CREATE PROCEDURE tmpfunc() BEGIN
  IF x THEN 
    INSERT INTO mytable (mycol)
    VALUES ('foo');
  END IF;
END \\
DELIMITER ;
CALL tmpfunc();
DROP PROCEDURE tmpfunc;

(as per this answer)

Community
  • 1
  • 1
Rich
  • 13,254
  • 1
  • 56
  • 102
2

you are looking for CASE WHEN? http://dev.mysql.com/doc/refman/5.0/en/case-statement.html

IF is already there in manual http://dev.mysql.com/doc/refman/5.0/en/if-statement.html

I guess @Nanne link is more relevant. Just adding it to the list of links here. http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

Nishant
  • 49,257
  • 12
  • 102
  • 116
0

MySQL several queries using if illustrates an approach that is probably relevant here.

Note that I have assumed that this question is asking "how can I use data from a table to direct control flow which might alter some other part of the database?"

Community
  • 1
  • 1
rdm
  • 570
  • 4
  • 15
0
    CASE
        WHEN receiver = '$userid' THEN receiver
        ELSE sender
    END AS contact
SuperSpy
  • 1,224
  • 3
  • 13
  • 28