{"id":3539,"date":"2015-05-25T22:34:01","date_gmt":"2015-05-26T06:34:01","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=3539"},"modified":"2023-12-28T22:00:22","modified_gmt":"2023-12-29T05:00:22","slug":"mysql-case-function","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/","title":{"rendered":"MySQL CASE Expression"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>CASE<\/code> expression to add if-else logic to queries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL CASE expression<\/h2>\n\n\n\n<p>MySQL <code>CASE<\/code> expression is a control flow structure that allows you to add if-else logic to a query. Generally speaking, you can use the <code>CASE<\/code> expression anywhere that allows a valid expression e.g., <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT<\/a><\/code>, <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-where\/\">WHERE<\/a><\/code> and <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\">ORDER BY<\/a><\/code> clauses.<\/p>\n\n\n\n<p>The <code>CASE<\/code> expression has two forms: simple <code>CASE<\/code> and searched <code>CASE<\/code>.<\/p>\n\n\n\n<p class=\"note\">Note that MySQL has a <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-case-statement\/\">CASE<\/a><\/code> statement that you can use only in stored programs such as stored procedures, stored functions, events, and triggers, which is not the <code>CASE<\/code> expression covered in this tutorial.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Simple CASE expression<\/h3>\n\n\n\n<p>The following illustrates the syntax of a simple&nbsp; <code>CASE<\/code> expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CASE value\n   WHEN value1 THEN result1\n   WHEN value2 THEN result2\n   \u2026\n   &#91;ELSE else_result]\n<span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, <code>CASE<\/code> matches the value with the <code>value1<\/code>, <code>value2<\/code>, etc., for equality and return the corresponding <code>result1<\/code>, <code>result2<\/code>,&#8230; If the <code>value<\/code> does not equal to any <code>value1<\/code>, <code>value2<\/code>, &#8230; <code>CASE<\/code> returns the result in the <code>ELSE<\/code> clause if the <code>ELSE<\/code> clause is specified.<\/p>\n\n\n\n<p>The <code>CASE<\/code> compares the <code>value<\/code> with <code>values<\/code> in the <code>WHEN<\/code> clauses for equality, you cannot use it with <code>NULL<\/code> because <code>NULL = NULL<\/code> returns false.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Searched CASE expression<\/h3>\n\n\n\n<p>The following shows the syntax of a searched <code>CASE<\/code> expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CASE\n   WHEN expression1 THEN result1\n   WHEN expression2 THEN result2\n   \u2026\n   &#91;ELSE else_result]\n<span class=\"hljs-keyword\">END<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>CASE<\/code> evaluates expressions specified in the <code>WHEN<\/code> clauses. If an expression evaluates to true. CASE returns the corresponding result in the <code>THEN<\/code> clause. Otherwise, it returns the result specified in the <code>ELSE<\/code> clause. In case the <code>ELSE<\/code> clause is not available, then the <code>CASE<\/code> expression returns <code>NULL<\/code> .<\/p>\n\n\n\n<p>The <code>CASE<\/code> expression returns a result whose <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-data-types\/\">data type<\/a> depends on the context where it is used. For example, if the <code>CASE<\/code> expression is used in the character string context, it returns the result as a character string. If the <code>CASE<\/code> expression is used in a numeric context, it returns the result as an integer, a decimal, or a real value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL CASE expression examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using CASE expression in the SELECT clause example<\/h3>\n\n\n\n<p>See the following <code>orders<\/code> and <code>customers<\/code> tables:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"304\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png\" alt=\"\" class=\"wp-image-8036\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png 434w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders-200x140.png 200w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/figure>\n\n\n\n<p>The following statement returns the customers and their orders:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    customerName, \n    <span class=\"hljs-keyword\">COUNT<\/span>(*) orderCount\n<span class=\"hljs-keyword\">FROM<\/span>\n    orders\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> customers \n\t<span class=\"hljs-keyword\">USING<\/span> (customerNumber)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> customerName\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">COUNT<\/span>(*);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"303\" height=\"300\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-simple-CASE-expression-example.png\" alt=\"MySQL simple CASE expression example\" class=\"wp-image-8743\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-simple-CASE-expression-example.png 303w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-simple-CASE-expression-example-200x198.png 200w\" sizes=\"auto, (max-width: 303px) 100vw, 303px\" \/><\/figure>\n\n\n\n<p>This example uses the <code>CASE<\/code> expression in the <code>SELECT<\/code> clause to return the type of customers based on the number of orders that customers ordered:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> cte <span class=\"hljs-keyword\">AS<\/span> (\n\t<span class=\"hljs-keyword\">SELECT<\/span> \n\t\tcustomerName, \n\t\t<span class=\"hljs-keyword\">COUNT<\/span>(*) orderCount\n\t<span class=\"hljs-keyword\">FROM<\/span>\n\t\torders\n\t<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> customers \n\t\t<span class=\"hljs-keyword\">USING<\/span> (customerNumber)\n\t<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> customerName\n)\n<span class=\"hljs-keyword\">SELECT<\/span> \n    customerName, \n    orderCount,\n    <span class=\"hljs-keyword\">CASE<\/span> orderCount\n\t\t<span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-string\">'One-time Customer'<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-string\">'Repeated Customer'<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-string\">'Frequent Customer'<\/span>\n        <span class=\"hljs-keyword\">ELSE<\/span> <span class=\"hljs-string\">'Loyal Customer'<\/span>\n\t<span class=\"hljs-keyword\">end<\/span> customerType\n<span class=\"hljs-keyword\">FROM<\/span>\n    cte\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> customerName;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"442\" height=\"336\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-SELECT-clause.png\" alt=\"MySQL CASE expression with SELECT clause\" class=\"wp-image-8742\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-SELECT-clause.png 442w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-SELECT-clause-200x152.png 200w\" sizes=\"auto, (max-width: 442px) 100vw, 442px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using CASE expression in the ORDER BY clause example<\/h3>\n\n\n\n<p>The following example uses the <code>CASE<\/code> expression to sort customers by state if the state is not <code>NULL<\/code>,\u00a0 or sort the country in case the state is <code>NULL<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    customerName, \n    state, \n    country\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> (\n    <span class=\"hljs-keyword\">CASE<\/span>\n\t<span class=\"hljs-keyword\">WHEN<\/span> state <span class=\"hljs-keyword\">IS<\/span> <span class=\"hljs-literal\">NULL<\/span> \n            <span class=\"hljs-keyword\">THEN<\/span> country\n\t<span class=\"hljs-keyword\">ELSE<\/span> state\n<span class=\"hljs-keyword\">END<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-case-function\/#1\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"314\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-ORDER-BY-clause.png\" alt=\"MySQL CASE expression with ORDER BY clause\" class=\"wp-image-8738\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-ORDER-BY-clause.png 270w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-ORDER-BY-clause-172x200.png 172w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using CASE expression with an aggregate function example<\/h3>\n\n\n\n<p>The following example uses the <code>CASE<\/code> expression with the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/\">SUM()<\/a><\/code> function to calculate the total of sales orders by order status:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Shipped'<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">ELSE<\/span> <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">END<\/span>) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-string\">'Shipped'<\/span>,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'On Hold'<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">ELSE<\/span> <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">END<\/span>) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-string\">'On Hold'<\/span>,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'In Process'<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">ELSE<\/span> <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">END<\/span>) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-string\">'In Process'<\/span>,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Resolved'<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">ELSE<\/span> <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">END<\/span>) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-string\">'Resolved'<\/span>,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Cancelled'<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">ELSE<\/span> <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">END<\/span>) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-string\">'Cancelled'<\/span>,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Disputed'<\/span> <span class=\"hljs-keyword\">THEN<\/span> <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">ELSE<\/span> <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">END<\/span>) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-string\">'Disputed'<\/span>,\n    <span class=\"hljs-keyword\">COUNT<\/span>(*) <span class=\"hljs-keyword\">AS<\/span> Total\n<span class=\"hljs-keyword\">FROM<\/span>\n    orders;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-case-function\/#2\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"53\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-aggregate-function-example.png\" alt=\"MySQL CASE expression with aggregate function example\" class=\"wp-image-8740\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-aggregate-function-example.png 500w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-CASE-expression-with-aggregate-function-example-200x21.png 200w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code>CASE<\/code> statement returns 1 if the status equals the corresponding status such as Shipped, on hold, in Process, Cancelled, Disputed, and zero otherwise.<\/li>\n\n\n\n<li>Second, the <code>SUM()<\/code> function returns the total number of orders per order status.<\/li>\n<\/ul>\n\n\n\n<p>In this tutorial, you have learned how to use the MySQL <code>CASE<\/code> expression to add if-else logic to the queries.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3539\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/\"\n\t\t\t\tdata-post-title=\"MySQL CASE Expression\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3539\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/\"\n\t\t\t\tdata-post-title=\"MySQL CASE Expression\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the MySQL CASE expression to add if-else logic to queries.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":7030,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-3539","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL CASE Expressions<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use the MySQL CASE expression to add if-else logic to SELECT, WHERE, and ORDER BY of a query.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL CASE Expressions\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use the MySQL CASE expression to add if-else logic to SELECT, WHERE, and ORDER BY of a query.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-29T05:00:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/mysql-case-function\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/mysql-case-function\\\/\",\"name\":\"MySQL CASE Expressions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/mysql-case-function\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/mysql-case-function\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"datePublished\":\"2015-05-26T06:34:01+00:00\",\"dateModified\":\"2023-12-29T05:00:22+00:00\",\"description\":\"In this tutorial, you will learn how to use the MySQL CASE expression to add if-else logic to SELECT, WHERE, and ORDER BY of a query.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/mysql-case-function\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/mysql-case-function\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/mysql-case-function\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"width\":434,\"height\":304},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/mysql-case-function\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Control Flow Functions and Expressions\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-control-flow-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL CASE Expression\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL CASE Expressions","description":"In this tutorial, you will learn how to use the MySQL CASE expression to add if-else logic to SELECT, WHERE, and ORDER BY of a query.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/","og_locale":"en_US","og_type":"article","og_title":"MySQL CASE Expressions","og_description":"In this tutorial, you will learn how to use the MySQL CASE expression to add if-else logic to SELECT, WHERE, and ORDER BY of a query.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-29T05:00:22+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/","url":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/","name":"MySQL CASE Expressions","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","datePublished":"2015-05-26T06:34:01+00:00","dateModified":"2023-12-29T05:00:22+00:00","description":"In this tutorial, you will learn how to use the MySQL CASE expression to add if-else logic to SELECT, WHERE, and ORDER BY of a query.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","width":434,"height":304},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Control Flow Functions and Expressions","item":"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/"},{"@type":"ListItem","position":3,"name":"MySQL CASE Expression"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=3539"}],"version-history":[{"count":1,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3539\/revisions"}],"predecessor-version":[{"id":13706,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/3539\/revisions\/13706"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/7030"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=3539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}