1.case…when…else…end
语法
CASE WHEN condition THEN result
[WHEN ...]
[ELSE result]
END
示例
case
when (xxx = '1') then 1
when (xxx = '2') then 2
when (xxx = '3') then 3
else 0
end as xxx
2.判断字符串是否为数字,是则转为数字
case
when (xxx = '1') then 1
when (xxx = '2') then 2
when (xxx = '3') then 3
when (xxx ~ '^([0-9]?[0-9]+)$') then to_number(xxx, '999')
else 0
end as priority
其中,xxx ~ '^([0-9]?[0-9]+)$'
的~
为正则表达式,to_number(xxx, '999')
方法中第一个参数为表中字段,第二个参数为数字的规则,999代表三位数字,但第二个参数必须为字符串。
3.coalesce和ilike的使用
select coalesce(user_name,'') from table_user where user_id ilike '%fracong%'
说明:
- 1.返回
user_name
的时候,如果user_name
为null,则设置为’’; - 2.
ilike
的使用:- 2.1.不区分大小写,而
like
是区分大小写的. - 2.2.ilike可以和以下SQL进行替换,其中
?
为user_id
- 2.1.不区分大小写,而
select coalesce(user_name,'') from table_user where (coalesce(?, '') = '' or upper(user_id) like (concat('%', upper(?), '%')))
4.substring的使用
截取语法:substring(filed from start_num for length)
start_num
为开始的位置(从1开始计数,包含1;如果为负数,则还是从1开始,length的长度相应递减),length
为截取的长度。
如下例子:
select user_name from user_table when user_name = '22fracong111';
结果:22fracong111
select substring(user_name from 3 for 9) from user_table when user_name = '22fracong111';
结果:fracong
5.对列进行操作
5.1.删除列
alter table table_name drop column column_name;
alter table table_name drop column column_name cascade;//删除外健依赖
5.2.新增列
alter table table_name add column column_name varchar(10);
alter table table_name add column column_name integer;
alter table table_name add column column_name varchar(10) not null;
alter table table_name add column column_name varchar(10) default '';