![]() Follow C# Corner to learn more new and amazing things about MySQL. ![]() In this article, I have discussed the concept of CASE Expressions in MySQL with various examples. WHEN "In Progress" THEN "Product_Not_Shipped" END AS ProductStatus With Aggregate Function SELECT SUM(CASE WHEN Status = 'Done'ĭifference between SIMPLE and SEARCHED CASE ExpressionsĮxample 1 SELECT CASE WHEN 10 * 2 = 20 THEN '20 correct' But, if any value is not satisfied by the corresponding conditions then, it returns the “result_value” that is specified in the ELSE clause.Įxample 1 SELECT CASE WHEN 10 * 2 = 25 THEN 'Incorrect'Įxample 2 SELECT CASE WHEN 5 * 5 - 5 / 5 + 6 = 30 THEN "TRUE"Įxample 3 SELECT CASE WHEN 10 * 2 = 20 THEN '20 correct'Įxample 5. In the Searched Case Expressions, CASE evaluates the “expressions” values that are specified in the WHEN clause and returns the corresponding “result_set” value. THEN 1 ELSE 0 END) AS 'Product_Not_Shipped', With Aggregate Function SELECT SUM(CASE Status WHEN 'Done' But, if the “case_value” does not match with any “value,” then it returns the “result_value”.Įxample 1 SELECT CASE 10 * 2 WHEN 25 THEN 'Incorrect'Įxample 2 SELECT CASE 5 * 5 - 5 / 5 + 6 WHEN 30 THEN "TRUE"Įxample 5. ![]() In the Simple Case Expressions, when “case_value” matches with its respective “value”, then it returns the corresponding “result” value. Here, I am providing the database with the tables containing the records, on which I am showing you various examples. ![]() MySQL CASE Statements are only used in Stored Procedures, Functions, Events, and Triggers, whereas MySQL CASE Expressions are used in SELECT Queries.įirst, let's create a database with a few tables containing some dummy data. Note that MySQL CASE Expressions are different from MySQL CASE Statements. You can use the CASE expression anywhere in a query, like SELECT, WHERE, or ORDER BY clause. If we have to use it in a numeric context, the result is returned as a decimal, real, or integer value. An expression is the compatible aggregated type of all return values but also depends on the context. In MySQL, the case expression shows multiple conditions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |