CASE in Mysql

CASE in Mysql

CASE in Mysql:
This function used to select particular Case. If there was no matching result value, the result after ELSE is returned.The default return type of a CASE expression is the compatible aggregated type of all return values, but also depends on the context in which it is used. If used in a string context, the result is returned as a string. If used in a numeric context, then the result is returned as a decimal, real, or integer value.

Syntax :

CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result …] [ELSE result] END

Example :
SELECT CASE ‘a’
WHEN ‘a’ THEN
(select count(*) from a_wordlist )
WHEN ‘b’ THEN
(select count(*) from b_wordlist )
ELSE ‘SELECT a or b’ END;

Output:

750 (a_wordlist count)

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.