在本篇文章中将给大家讲解下CASE的其他用法讲解:

使用带有简单CASE函数和CASE搜索函数的select语句在select语句中,CASE搜索函数允许根据比较值

select

CASE

WHEN good_type<2

THEN '<2'

WHEN good_type>=2 AND good_type<3

THEN '>=2 && <3'

ELSE '>=3' END AS good_now_type,

good_type,user_id,user_name

FROM t_main_order;

+---------------+-----------+---------+-----------+

| good_now_type | good_type | user_id | user_name |

+---------------+-----------+---------+-----------+

| <2 | 0 | 1 | tina |

| <2 | 0 | 2 | tige |

| <2 | 1 | 3 | five |

| >=2 && <3 | 2 | 4 | wate |

| >=3 | 3 | 5 | fiww |

| >=3 | 3 | 6 | www |

| >=3 | 3 | 7 | wfiw |

+---------------+-----------+---------+-----------+

CASE的其他用法

select

CASE

WHEN good_type<2

THEN '<2'

WHEN good_type>=2 AND good_type<3

THEN '>=2 && <3' ELSE '>=3'

END AS good_now_type,

count(*) AS num_count

FROM t_main_order

GROUP BY good_now_type

ORDER BY num_count;

+---------------+-----------+

| good_now_type | num_count |

+---------------+-----------+

| >=2 && <3 | 1 |

| <2 | 3 |

| >=3 | 3 |

+---------------+-----------+