数据查询

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 outerselect * 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)






点赞(1) 打赏

评论列表 共有 0 条评论

暂无评论
意见
建议
发表
评论
返回
顶部