数据查询
SQL 作为数据查询语言(DQL:Data Query Language),其语句也称为“数据检索语句”,用以从表中获得数据,确定数据怎样在应用程序给出。不同用户群体对 SQL 的侧重点是有差异的,但无论是哪一个群体,基本都绕不开数据查询语句,是最为基础的内容。
基础查询: 基础的查询语句Select,其实就是简单的跟数据库对话的过程。不管是哪种 SQL 的拓展语言,基础查询里的语法基本都一致。如*代表全量查询, distinct 代表去重,top 和 limit 代表数据条数限定, as 代表原表字段名进行替换更新。
名称 | 注释 | 使用方法 |
select | 基础查询 | select * from table |
distinct | 去重查询 | select distinct column1,column2 from table |
limit | 限制查询数量 | select * from table limit 10 |
as | 替换原字段名 | select tab_name as name from table |
like | 正则匹配查询 | select * from table where namt like '%张三%' |
in | 限定一个范围 | select * from table where id in ('1,2,3,4') |
not | 反向查询 | select * from table where id not in ('1,2,3,4') |
order by | 查询数据排序,desc 降序 asc 升序 | select * from table limit 10 border by id desc |
count(*) | 计数查询 | select count(*) from table |
min | 查询最小数 | select min(age) from table |
max | 查询最大数 | select max(age) from table |
avg | 查询平均数 | select avg(age) from table |
sun | 查询合计数 | select sun(age) from table |
group by | 分组查询 | select deptno,sum(sal) from emp group by deptno |
joins | 多表联合查询,left、right、inner、full outer | select * from fa_user as a left join fa_tab asb on a.id=b.uid |
insert into | 插入数据 | insert into table (id,name,age) values ('1','张三','18') |
update | 修改数据 | update table set id =2,name='李思',age='26' where id =1 |
delete | 删除数据 | delete from table where id=2 |
常用函数
名称 | 注释 | 使用方法 | |
ifnull | 函数用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。 | ifnull(name,0) | |
group_concat | 函数是将归类后的名字以逗号连接成字符串 | group_concat(name) | |
from_unixtime | 函数将时间戳转换成指定的日期格式 | from_unixtime(time,%Y-%m-%d %H:%i:%s) | |
round | 四舍五入 | round(score) | |
ceil | 向上取整 | ceil(score) | |
floor | 向下取整 | floor(score) | |
concat | 函数将两个字符串拼接在一起 | concat('用户id是',id) |
发表评论 取消回复