PL/SQL 使用控制流程 https://www.cnblogs.com/springsnow/p/9394892.htmlhtml
SELECT grade, COUNT (CASE WHEN sex = 1 THEN 1 ELSE NULL END) 男生數, COUNT (CASE WHEN sex = 2 THEN 1 ELSE NULL END) 女生數 FROM students GROUP BY grade;
Case函數不一樣於DECODE函數。在Case函數中,能夠使用BETWEEN,LIKE,IS NULL,IN,EXISTS等等。好比說使用IN,EXISTS,能夠進行子查詢,從而 實現更多的功能。spring
SELECT keyCol, CASE WHEN keyCol IN ( SELECT keyCol FROM tbl_B ) THEN 'Matched' ELSE 'Unmatched' END Label FROM tbl_A;
也能夠使用NOT IN和NOT EXISTS,可是這個時候要注意NULL的狀況。函數
UPDATE PersonnelSET salary = CASE WHEN salary >= 5000 THEN salary * 0.9 WHEN salary >= 2000 AND salary < 4600 THEN salary * 1.15 ELSE salary END;
SELECT * FROM TB_Test WHERE stateid IN ( 10, 20 ) AND ( CASE WHEN stateid = 20 AND userid <> 2 THEN 0 ELSE 1 END ) = 1
SELECT CASE WHEN salary <= 500 THEN '1' WHEN salary > 500 AND salary <= 600 THEN '2' WHEN salary > 600 AND salary <= 800 THEN '3' ELSE NULL END salary_class, -- 別名命名 COUNT(*) FROM Table_A GROUP BY CASE WHEN salary <= 500 THEN '1' WHEN salary > 500 AND salary <= 600 THEN '2' WHEN salary > 600 AND salary <= 800 THEN '3' ELSE NULL END;