SQL foundation tutorial (2nd edition) Chapter 6 function, predicate, CASE expression: 6-3 CASE expression

Although the CASE expression in the ELSE clause can be omitted, but in order to make the SQL statements easier to understand, or that we should not be omitted. 
the CASE expression END can not be omitted.
Use CASE expression can be SELECT statement are combined.


What is CASE expression
CASE expression is used in distinguishing cases, distinguished in such a case generally referred to programming (conditional) branch .

CASE expression syntax
Let's hurry to learn about search CASE expression syntax right.

CASE use expressions
ELSE clause also can be omitted, then will default to ELSE NULL . However, in order to prevent the leakage was read, still I hope that we can show to write  ELSE  clause.
 

 

--MySQL
-- MySQL中使用IF代替CASE表达式
SELECT  product_name,
        IF( IF( IF(product_type = '衣服',  CONCAT('A:', product_type), NULL)
                    IS NULL AND product_type = '办公用品', CONCAT('B:', product_type), 
                IF(product_type = '衣服',  CONCAT('A:', product_type), NULL))
                    IS NULL AND product_type = '厨房用具', CONCAT('C:', product_type), 
                    IF( IF(product_type = '衣服',  CONCAT('A:', product_type), NULL)
                    IS NULL AND product_type = '办公用品', CONCAT('B:', product_type), 
                IF(product_type = '衣服',  CONCAT('A:', product_type), NULL))) AS abc_product_type
  FROM Product;

CASE表达式的书写位置

 

Guess you like

Origin www.cnblogs.com/MarlonKang/p/12232972.html