關於sql中單引號和雙引號的使用,來一點說明:
1. 查詢列的別名如果含有漢字或者特殊字符(如以'_'開頭),需要用雙引號引起來。而且只能用雙引號,單引號是不可以的。
2. 如果想讓某列返回固定的值,而且這個返回值是varchar類型的,返回值需要用單引號引起來。而且只能用單引號,雙引號是不可以的。
例子1:
a)查詢列中有"_"開頭的列名時,需要用雙引號引起來。
b)使用"case"關鍵字,根據分數直接判斷是否及格。
create table tbl_score( id NUMBER(4), --id name varchar2(30), --名稱 score NUMBER(3), --分數 otherscore NUMBER(3) --其他分數 );
select t.id as "_id", t.name as name, case when t.score >= 60 then '及格' else '不及格' end as "是否及格", t.score as "分數" from tbl_score t;
例子2:
根據情況返回不同列的值。
同用上面的tbl_score表。
select t.id as "_id", t.name as name, case when t.score is null then t.otherscore --也可返回固定值 else t.score end as "分數", t.score, t.otherscore from tbl_score t;