{"id":10137,"date":"2024-04-13T22:12:28","date_gmt":"2024-04-13T19:12:28","guid":{"rendered":"https:\/\/sunucun.com.tr\/bilgi\/?post_type=dt_articles&#038;p=10137"},"modified":"2026-02-06T21:49:50","modified_gmt":"2026-02-06T18:49:50","slug":"using-case-when-in-mysql-returning-different-values-based-on-conditions","status":"publish","type":"post","link":"https:\/\/sunucun.com.tr\/blog\/using-case-when-in-mysql-returning-different-values-based-on-conditions\/","title":{"rendered":"Using CASE WHEN in MySQL: Returning Different Values Based on Conditions"},"content":{"rendered":"<p><!-- SEO Meta Description --><\/p>\n<p>MySQL CASE WHEN yap\u0131s\u0131n\u0131 kullanarak ko\u015fullara g\u00f6re farkl\u0131 de\u011ferler d\u00f6nd\u00fcrme i\u015flemlerini \u00f6\u011frenin. Bu makale, MySQL CASE WHEN kullan\u0131m\u0131n\u0131 ad\u0131m ad\u0131m a\u00e7\u0131klamaktad\u0131r.<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>MySQL, being one of the most popular relational database management systems, is widely used across various industries. During database querying, there are instances where it&#8217;s necessary to return different values based on specific conditions. In such cases, MySQL&#8217;s CASE WHEN structure comes in handy. This article, titled &#8220;<a href=\"https:\/\/sunucun.com.tr\/blog\/using-case-when-in-mysql-returning-different-values-based-on-conditions\/\">Using CASE WHEN in MySQL: Returning Different Values Based on Conditions<\/a>,&#8221; explores how to use CASE WHEN in MySQL to return different values based on conditions. More specifically, it will provide you with a comprehensive guide on implementing this useful feature in your database operations.<\/p>\n<p><strong>What is the CASE WHEN Structure?<\/strong><\/p>\n<p>The CASE WHEN structure in MySQL is used to return different values based on specific conditions. This structure is typically employed within SELECT queries or UPDATE operations. Similar to an if-else statement, the CASE WHEN structure evaluates whether one or more conditions are met and returns the corresponding results. This functionality makes your queries more dynamic and adaptable, allowing you to cater to various scenarios within a single SQL statement.<\/p>\n<p><strong>Using CASE WHEN<\/strong><\/p>\n<p>Utilizing the CASE WHEN structure to return different values based on conditions is straightforward. Here&#8217;s the basic syntax:<\/p>\n<pre class=\"wp-block-code\"><code>SELECT \r\n    CASE \r\n        WHEN condition1 THEN result1\r\n        WHEN condition2 THEN result2\r\n        ...\r\n        ELSE default_result\r\n    END \r\n    AS column_alias\r\nFROM \r\n    your_table;\r\n<\/code><\/pre>\n<p>In the above syntax:<\/p>\n<ul class=\"wp-block-list\">\n<li>Conditions such as &#8220;condition1&#8221; and &#8220;condition2&#8221; are evaluated.<\/li>\n<li>The result corresponding to the first satisfied condition, such as &#8220;result1&#8221; or &#8220;result2&#8221;, is returned.<\/li>\n<li>If none of the conditions are met, the default value specified as &#8220;default_result&#8221; is returned.<\/li>\n<\/ul>\n<p>This powerful structure allows for greater flexibility and control over the output of your queries, enabling you to handle complex logic directly within your SQL statements. For example, if you&#8217;re managing a large dataset with multiple categories, the CASE WHEN structure can simplify your analysis by grouping and categorizing data in a single query.<\/p>\n<figure class=\"wp-block-image size-large is-resized\"><img fetchpriority=\"high\" fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.sunucun.com.tr\/blog\/\/wp-content\/uploads\/2024\/04\/Uninstall-MySQL-Ubuntu-1-1024x576.jpg\" alt=\"Example of MySQL CASE WHEN Structure\" class=\"wp-image-10134\" style=\"width:748px;height:auto\" srcset=\"https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2024\/04\/Uninstall-MySQL-Ubuntu-1-1024x576.jpg 1024w, https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2024\/04\/Uninstall-MySQL-Ubuntu-1-300x169.jpg 300w, https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2024\/04\/Uninstall-MySQL-Ubuntu-1-768x432.jpg 768w, https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2024\/04\/Uninstall-MySQL-Ubuntu-1.jpg 1200w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p><strong>Examples of Using CASE WHEN<\/strong><\/p>\n<p>Let&#8217;s consider a few examples to illustrate the usage of CASE WHEN in different scenarios:<\/p>\n<ul class=\"wp-block-list\">\n<li><strong>Example 1: Evaluating a student&#8217;s performance based on their grade:<\/strong><\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code>SELECT \r\n    student_name,\r\n    CASE \r\n        WHEN grade >= 90 THEN 'AA'\r\n        WHEN grade >= 80 THEN 'BA'\r\n        WHEN grade >= 70 THEN 'BB'\r\n        WHEN grade >= 60 THEN 'CB'\r\n        WHEN grade >= 50 THEN 'CC'\r\n        ELSE 'FF'\r\n    END \r\n    AS performance\r\nFROM \r\n    students;\r\n<\/code><\/pre>\n<p>In this query, a student&#8217;s performance is evaluated based on their grade, and the corresponding letter grade is returned. This approach is particularly useful in educational settings where grading systems need to be consistently applied across various students or assessments.<\/p>\n<ul class=\"wp-block-list\" start=\"2\">\n<li><strong>Example 2: Applying discounts based on a customer&#8217;s total purchase amount:<\/strong><\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code>SELECT \r\n    customer_name,\r\n    total_amount,\r\n    CASE \r\n        WHEN total_amount >= 1000 THEN total_amount * 0.1\r\n        WHEN total_amount >= 500 THEN total_amount * 0.05\r\n        ELSE total_amount\r\n    END \r\n    AS discounted_amount\r\nFROM \r\n    orders;\r\n<\/code><\/pre>\n<p>This query calculates discounts based on a customer&#8217;s total purchase amount and returns the discounted amount accordingly. Businesses can use this method to automatically apply discount rules within their <a href=\"https:\/\/sunucun.com.tr\/en\/contact\" data-internallinksmanager029f6b8e52c=\"167\" title=\"Contact Sunucun support and sales\">sales<\/a> databases, ensuring that customers receive the appropriate price reductions based on their spending levels.<\/p>\n<ul class=\"wp-block-list\" start=\"3\">\n<li><strong>Example 3: Categorizing products based on stock levels:<\/strong><\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code>SELECT \r\n    product_name,\r\n    stock_quantity,\r\n    CASE \r\n        WHEN stock_quantity > 100 THEN 'High Stock'\r\n        WHEN stock_quantity BETWEEN 50 AND 100 THEN 'Medium Stock'\r\n        WHEN stock_quantity BETWEEN 1 AND 49 THEN 'Low Stock'\r\n        ELSE 'Out of Stock'\r\n    END \r\n    AS stock_status\r\nFROM \r\n    products;\r\n<\/code><\/pre>\n<p>In this scenario, the query categorizes products based on their stock levels, providing a quick overview of inventory status. This method is invaluable for inventory management, helping businesses to prioritize restocking and identify products that are at risk of selling out.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>In this article, we&#8217;ve learned how to use the CASE WHEN structure in MySQL to return different values based on conditions. The CASE WHEN structure enhances the flexibility of your database queries, allowing you to achieve various results based on different conditions. By leveraging this structure, you can exercise greater control over your database operations and obtain the desired outcomes. Whether you are managing a grading system, applying discounts, or categorizing products, the CASE WHEN structure in MySQL is a powerful tool that can simplify and optimize your SQL queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL CASE WHEN yap\u0131s\u0131n\u0131 kullanarak ko\u015fullara g\u00f6re farkl\u0131 de\u011ferler d\u00f6nd\u00fcrme i\u015flemlerini \u00f6\u011frenin. Bu makale, MySQL CASE WHEN kullan\u0131m\u0131n\u0131 ad\u0131m ad\u0131m a\u00e7\u0131klamaktad\u0131r. Introduction MySQL, being one of the most popular relational database management systems, is widely used across various industries. During database querying, there are instances where it&#8217;s necessary to return different values based on specific&hellip;<\/p>\n","protected":false},"author":1,"featured_media":10135,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1525,1521],"tags":[],"class_list":["post-10137","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-siber-guvenlik","category-teknoloji"],"_links":{"self":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/10137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=10137"}],"version-history":[{"count":2,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/10137\/revisions"}],"predecessor-version":[{"id":19791,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/10137\/revisions\/19791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media\/10135"}],"wp:attachment":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=10137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=10137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=10137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}