Sql 笔记
”where 1=1“用于动态sql拼接
查询运算符'<>' 不等于
数字函数:
abs();//绝对值
power();//指数
sqrt();//平方根
raud();// 随机(Mysql) dbms_random.value随机数(oracle)
mod();//余数
字符串函数:
length();
Lower();//小写 Upper()//大写
drop 与 delete
前者不只是删除数据,数据结构也会删除
而后者只是删除数据
order by 多字段排序
会根据优先级字段内排序
注意:order by 与where 共用时必须放到where后面
group by 分组查询
注意事项:1、必须放到select 或者where 之后
2、没有出现在group by 后或者没有使用聚合函数的字段不能出现在使用group by语句中select之后
3、Having条件要紧跟group by 之后 且条件不能包含为分组的字段
分页
Mysql:
select ……order by……Limit 起始行,结尾行
MSSQLserver:
select top 行数……
row_number()函数 select * from (select row_number() over(order by id asc) as rownum,name,dept from student ) as a where a.rownum>=3 and a.rownum<=10 //取3到10行的数据
Oracle:
select * from (select rownum as row_num,v_dict_name from t_sys_dict) t where row_num>=5 and row_num<=10; //oracle 同样支持rownum函数
字符串拼接函数
concat();
select concat('id为:',id,'姓名为:',name) from student
union & union all
select name,age from student
union
select '平均年龄',avg(age) from student
结果展示:
张三 21
王五 23
平均年龄 22
Mysql:
取系统时间:
now(); sysdate(); current_timestamp;
求时间差:
datediff(from_date,to_date);
cast(date_cloumnName as type)
concat(from_date,date_type)
简单查询转换:
select cast(birthday as char) as birthday from distinctss
范围查询:
select * from table_name where 字段名 between '' and ''