ifnull
IFNULL(expr1,expr2)
如果 expr1 不是 NULL,IFNULL() 返回 expr1,否则它返回 expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。
SQL日期格式化
DATE_FORMAT(date,format)
SELECT date_format(date_add(now(), INTERVAL-1DAY), '%Y-%m-%d');
SELECT date_format(now(), '%Y-%m-%d'));
format参数的格式
%a 缩写星期名
%b 缩写月名
%c 月,数值
%D 带有英文前缀的月中的天
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%f 微秒
%H 小时 (00-23)
%h 小时 (01-12)
%I 小时 (01-12)
%i 分钟,数值(00-59)
%j 年的天 (001-366)
%k 小时 (0-23)
%l 小时 (1-12)
%M 月名
%m 月,数值(00-12)
%p AM 或 PM
%r 时间,12-小时(hh:mm:ss AM 或 PM)
%S 秒(00-59)
%s 秒(00-59)
%T 时间, 24-小时 (hh:mm:ss)
%U 周 (00-53) 星期日是一周的第一天
%u 周 (00-53) 星期一是一周的第一天
%V 周 (01-53) 星期日是一周的第一天,与 %X 使用
%v 周 (01-53) 星期一是一周的第一天,与 %x 使用
%W 星期名
%w 周的天 (0=星期日, 6=星期六)
%X 年,其中的星期日是周的第一天,4 位,与 %V 使用
%x 年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y 年,4 位
%y 年,2 位
sql拼接
mysql中用concat,oracle中concat和||都有,都是做字符串拼接的
--模糊查询
SELECT * from tab1 t where t.col1 like '%a%';
--模糊查询
SELECT * from tab1 t where t.col1 like '%' || 'a' || '%';
--模糊查询
SELECT * from tab1 t where t.col1 like concat(concat('%','a'),'%');
SELECT col1||col2||col3||col4 "Concatenation" FROM tab1;
--也就是当我们需要自定义查询结果的时候,使用||拼接。
SELECT 'col1=' || t.col1 || ',col2=' || t.col2 "字段拼接" FROM tab1 t;
应用
select OBJ_NAME as name,
sum(SCAN_NUM) as count
from adm_OBJ_SCAN_STAT
where OBJ_NAME like concat('%', concat(ifnull(:name,''), '%'))
and SCAN_DATE like concat('%', concat(ifnull(:date,''), '%'))
group by OBJ_NAME;