Sunday, 28 Sep 2008 7:18 EST

MySQL Stored Procedures: CASE Statements

Many developers aren't aware that there is a CASE statement in MySQL 5.0 which enables you to have if-then-else functionality directly in your SQL. The CASE statement is for stored procedures, which are SQL statements that can be stored on the server so that clients don't have to keep reissuing the same statements ove rand over. The functionality of the MySQL CASE statement is basically the same as any other case function in programming. If a search condition evaluates as true, then the following block of code is executed. You can have any number of if-then statements and follow them up with a single else statement to be executed if none of the search conditions are true. Be sure to always add an ELSE clause into your code block. MySQL will return the "Case not found for CASE statement" error if no search conditions evaluate to true and there is no ELSE clause. For instances where there is no ELSE code to execute, use an empty BEGIN . . . END code block. The CASE function is one of the most powerful functions in SQL coding. The CASE function can evaluate complex conditional statements, use any comparison operator and check multiple conditions all directly in your SQL code. CASE allows you to combine multiple SQL queries into one powerful query, based on the conditions you lay out.

Let's take a look at the following example. Say we have a database of baseball statistics and we want to pull all batters statistics and group them by how good of a hitter they are.
DELIMITER //[1]

CREATE PROCEDURE battertype()
  BEGIN
    DECLARE batting_avg INT DEFAULT .000;

    CASE batting_avg
      WHEN batting_avg > .300 THEN 'Great Hitter'
      WHEN batting_avg >= .275 and batting_avg < .300 THEN 'Good Hitter'
      WHEN batting_avg >= .250 and batting_avg < .275 THEN 'Average Hitter'
      WHEN batting_avg < .250 THEN 'Minor Leaguer'
      ELSE
        BEGIN
        END;
    END CASE;
  END;
//
And that's it for a basic MySQL CASE statement. Stay tuned for an in-depth look at MySQL stored procedures as well as several other MySQL posts that I'm working on. As always, if you have a better technique or a tweak that you think improves on this code, please share it in the comments. Also, if you have a tip, a trick or an idea for my next post, please don't hesitate to contact me.
Bookmark and Share

3 Comments So Far

Wednesday, 06 January 2010 03:19
Thursday, 21 January 2010 11:44
Monday, 15 February 2010 09:37

Post a comment


If you are seeing this message, you are either using a screen reader or you have disabled CSS. Please do not fill in the following form field, titled "lastname", or your comment will not be saved. This field is only used to help prevent spam comments.


Welcome

JustinSpegele.com is where I share projects that I'm working on, php tutorials, web development tips and tricks, and random thoughts.

#MLS has a very odd notion of an all-star game. MLS all-stars vs. an EPL team? Still, should be very fun to watch. MLS 3 - Man United 2 1:41 PM Jul 28th from web
The New Digg And The Future Of Social News http://bit.ly/cM7Guq 10:44 AM Jun 29th from TweetMeme
so as of right now, the Big 10 has 12 teams and the Big 12 has 10 teams? http://sports.espn.go.com/ncaa/news/story?id=5276668 7:59 PM Jun 11th from web
heading to Las Vegas for #caworld in the morning 7:12 PM May 12th from web
Found out from CNN that I went to high school with a convicted terrorist who worked with Al-Qaeda. Not what I expected to see on the news 9:49 PM May 10th from web
View all posts on Twitter »

Friend me

Twitter Digg Facebook FriendFeed Del.icio.us iFanboy Last.fm Squidoo

Login

Username:

Password: