The first chapter magical sql 1-1 CASE expression

Statement Analysis

Both versions of the results are the same,
"Sex" column (field) if it is '1', that
it is the result of man; if it is '2', then the result is female.
Simple CASE expression as its name suggests, simple writing, but things can be achieved is limited.
CASE expression can write simple terms, search CASE expression can write, the book basically written search CASE expression.

#CASE 表达式的写法
#简单 CASE 表达式
SELECT
case
	sex 
	WHEN '1' THEN
	'男' 
	WHEN '2' THEN
	'女' ELSE '其他' 
END 
FROM
	casepopulation;

operation result:
Here Insert Picture Description

In the WHEN clause is found to be true,
truth value judgment CASE expression is aborted, and the remaining WHEN clause is ignored.
In order to avoid unnecessary confusion, pay attention to the condition of exclusive use WHEN clause.

■剩余的 WHEN 子句被忽略的写法示例
-- 例如,这样写的话,结果里不会出现“第二”
SELECT
CASE
WHEN
	sex IN ( '1','2' ) THEN
	'第一' 
	WHEN sex IN ( '2' ) THEN
	'第二' ELSE '其他' 
END 
FROM
	casepopulation;

operation result:
Here Insert Picture Description

When using the CASE expression, also need to pay attention to the following points

  • Note 1: The data type of the harmonization of the branches back
    though Needless to say this, but here is to emphasize:
    we must pay attention CASE expression in the various branches of the data types returned are the same.
    A branch return character, while other branches return a numeric wording is incorrect

- Note 2: Do not forget to write END

When using the CASE expression, a syntax error is most prone to forget to write END.

- Note 3: to develop the habit of writing ELSE clause

And END different, ELSE clause is optional, do not write it will not go wrong.
When not writing ELSE clause, the results of the CASE expression is NULL.
But do not write may cause "no grammatical errors, not the result of" difficult to trace the cause of this trouble, it is best to explicitly write the ELSE clause (even in the case of results is NULL).
After develop this habit, we will clearly see from the code under these conditions will generate NULL, and in the future when the code is modified can reduce errors.

Convert the existing numbering for the new ways and statistics

#统计结果
SELECT
CASE
	pref_name 
	WHEN '得到' THEN
	'华南' 
	WHEN '香川' THEN
	'华西' 
	WHEN '徐州' THEN
	'华中' 
	WHEN '北京' THEN
	'华北' ELSE '其他' 
	END as district,
	SUM( population ) as '人口'
FROM
	casepopulation 
GROUP BY district;
	
	
#查询人数
SELECT 
CASE WHEN population < 100 THEN '01'
WHEN population >= 100 AND  population <200 THEN '02'
WHEN population >= 200 AND  population <300 THEN '03'
WHEN population >= 300 THEN '04'
ELSE NULL END AS pop_class,
count(1) as cnt
FROM casepopulation
GROUP BY pop_class;

Yes, here's another name --district GROUP BY clause of the SELECT clause precisely defined columns.
But strictly speaking, such an approach is contrary to the rules of standard SQL.
Because the GROUP BY clause than the first SELECT statement is executed, so in the GROUP BY clause cited
by another name in the SELECT clause definition it is not allowed.
In fact, Oracle, DB2, SQL Server , an error occurs when using such an approach and other database.
But there are also database support this SQL statement,
for example, PostgreSQL and MySQL , this query can be executed smoothly. This is because these databases in case of a query, the SELECT clause will first scan the list, and column calculations.
But because this is a violation of the standard wording, so here I strongly recommend not to use.
However, such written SQL statement is indeed very simple, but also very good readability

Data matching between the table

sql statement Address Table
Here Insert Picture Description

#1-1-课程
-- 表的匹配 :使用 IN 谓词
SELECT
	course_name,
CASE
	WHEN course_id IN ( SELECT course_id FROM opencourses WHERE month = '200706' ) THEN
	'O' ELSE 'x' 
	END AS "6月",
CASE
	WHEN course_id IN ( SELECT course_id FROM opencourses WHERE month = '200707' ) THEN
	'O' ELSE 'x' 
	END AS "7月",
CASE
	WHEN course_id IN ( SELECT course_id FROM opencourses WHERE month = '200708' ) THEN
	'O' ELSE 'x' 
	END AS "8月" 
FROM
	coursemaster;
	
	
-- 表的匹配 :使用 EXISTS 谓词
SELECT
	cm.course_name,
CASE
	WHEN EXISTS ( SELECT course_id FROM opencourses oc WHERE MONTH = '200706' AND oc.course_id = cm.course_id ) THEN
	'O' ELSE 'X' 
	END AS '6月',
CASE
	WHEN EXISTS ( SELECT course_id FROM opencourses oc WHERE MONTH = '200707' AND oc.course_id = cm.course_id ) THEN
	'O' ELSE 'X' 
	END AS '7月',
CASE
	WHEN EXISTS ( SELECT course_id FROM opencourses oc WHERE MONTH = '200708' AND oc.course_id = cm.course_id ) THEN
	'O' ELSE 'X' 
	END AS '8月' 
FROM
	coursemaster cm;

Such a query is not polymerized and therefore do not need to sort, increase the month when the repair only
change the SELECT clause can be, and better scalability.
Whether you use IN or EXISTS, the result is the same, but in terms of performance,
EXISTS better. EXISTS sub-query can be used by the "month The, course_
the above mentioned id" Such a primary key index, so especially when more data in Table OpenCourses time
advantage

Guess you like

Origin blog.csdn.net/qq_35226176/article/details/89534386