ZBLOG

mysql case when

MySQL CASE WHEN statement is a conditional expression that evaluates a set of conditions and returns a value based on the result of the evaluation. The syntax is as follows:

  1. SELECT
  2. CASE
  3. WHEN condition1 THEN result1
  4. WHEN condition2 THEN result2
  5. ...
  6. ELSE default_result
  7. END AS column_alias
  8. FROM table_name;

The conditions can be any logical expression that evaluates to true or false, such as comparisons (>, <, =), functions (LIKE, IN), or other operators (AND, OR). The results can be any data type, including strings, numbers, or NULL.

Here’s an example of using the CASE WHEN statement to categorize products by price:

  1. SELECT product_name,
  2. CASE
  3. WHEN price <= 10 THEN 'Low'
  4. WHEN price > 10 AND price <= 20 THEN 'Medium'
  5. WHEN price > 20 THEN 'High'
  6. END AS price_category
  7. FROM products;

This query will return a list of product names with their corresponding price categories (Low, Medium, or High), based on the price ranges defined in the CASE WHEN statement.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=889

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?