一,DQL基础查询
DQL(Data Query Language)数据查询语言,可以单表查询,也可以多表查询
语法:
select 查询结果 from 表名 where 条件;
特点:
查询结果可以是:表中的字段,函数
查询的结果是一个虚拟的表格
1,查询结果处理:
查询特定列: select column1,column2 from table
全部列查询: select * from table
处理函数: select 函数 from table
函数分类:
单行函数:如concat,length,ifnull等,会对查询的每行进行处理
分组函数:做统计使用,又称为统计函数、分组函数,会将多行处理成一行
单行函数:
(1)字符函数:
1.length()获取参数值的字节个数(一个中文字符占3个字节,英文字符一个字母占一个字节)
2.char_length()获取参数值的字符个数
SELECT NAME,LENGTH(NAME),CHAR_LENGTH(NAME) FROM student
-- 这里查询了学生表中的姓名,姓名的字节长度,姓名的字符长度
3.concat(str1,str2,...)拼接字符串
SELECT CONCAT(num,':',NAME) FROM student -- 这里指给学号列添加了:以及姓名列
4.upper()/lower():将字符串变成大写/小写
SELECT UPPER(NAME),LOWER(NAME) FROM student
-- UPPER(NAME)转大写,LOWER(NAME)转小写,仅仅转换英文字母大小写
5.substring(column,position,length)截取字符串 位置从1开始
-- substring(列,开始的位置,截取长度) 注意开始的位置是从1开始,返回的是被截取的字符
SELECT SUBSTRING(NAME,1,1) FROM student
6.trim(column)去掉字符串前后的空格或子串,trim(指定子串 from column)
-- trim(列) 默认是取出前后的空格
SELECT NAME,TRIM(NAME) FROM student
-- trim('*' FROM NAME) 去掉前后指定的字符串
SELECT NAME,TRIM('*' FROM NAME) FROM student
7.replace(column,old,new)替换,替换所有的子串
-- replace(列,old,new)将列中的数据将旧的字符改为新的字符
SELECT REPLACE(NAME,'j','J') FROM student
(2)逻辑处理
case when 条件 then 结果1 else 结果2 end; 可以有多个when
-- case when 条件 then 条件成立执行 else 条件不成立 end; 可以有多个when,且必须完成否则代码报错,else后直接跟结果
select num,name,
(case when height>=1.70 then '高个子' else '非高个子' end)as height
from student
-- 这里是查询学号,姓名,以及当身高大于等于1.70时 返回高个子否则返回非高个子并作为列,应用于每一行
select num,name,
(case
when height>=1.70 then '高个子'
when height>=1.60 then '正常'
else '低个子'
end)as height
from student
ifnull(被检测值,默认值)函数检测是否为null,如果为null,则返回指定的值,否则返回原本的值
-- ifnull(列,'默认值')检测指定列的值是否为空,如果为空显示默认值
select num,name,ifnull(birthday,'暂未录入信息')as birthday from student-- 检测birthday列的值是否为空,如果为空则显示传入的字符串
-- 查询学生表的学号,姓名,以及查询某个列是否为空,如果不为空显示表中的信息,如果不为空显示'暂未录入信息'
if函数:if else的 效果 if(条件,结果1,结果2)
-- if(条件,结果1,结果2)
select num,name,if(height>=1.70,'高个子','非高个子')as height from student
-- 这里指查询学号,姓名
(3)数学函数
round(数值):四舍五入
ceil(数值):向上取整,返回>=该参数的最小整数
floor(数值):向下取整,返回<=该参数的最大整数
truncate(数值,保留小数位的位数):截断,小数点后截断到几位,不会进行四舍五入
mod(被除数,除数):取余,被除数为正,则为正;被除数为负则为负
(4)日期函数
now():返回当前系统时间(年月日时分秒)
curdate():返回当前系统日期(年月日)
date_format(日期列,格式):将日期转换为指定格式
datediff(big,small):返回两个日期相差天数
日期格式:
select num,name,now(),curdate() from student
-- 查询学生表的学号,姓名,当前系统日期(年月时分秒),当前的系统时间(年月日)
select num,name,date_format(birthday,'%Y')as birthday from student-- 将生日格式化
select num,name from student where date_format(birthday,'%Y') = '2008'-- 查询生日为2008年的某行数据
select num,name from student where date_format(birthday,'%Y-%m-%d') = '2008-09-08'-- 先将生日格式化,通过where 条件查询birthday为'2008-09-08的学号与姓名'
select num,name,datediff(curdate(),birthday)as borndays from student-- 查询今天与生日相差多少天
分组函数
功能:用作统计使用,又称为聚合函数或统计函数
sum(列):求和 返回给定列的合计
-- 查询了学生表的最高身高且为单行单列
select sum(height) from student
avg(列):平均值 返回给定列的平均值
select avg(height) from student -- 查询学生表的身高平均值
max(列):最大值 返回该列中的最大值
min(列):最小值 返回该列中的最小值
select max(height)as maxheight ,min(height)as minheight from student
count(列):计数 统计此列中的个数,如果列值为null,则不统计,一般使用*或主键
-- count(列) 统计该列总数 值如果为null,不计算
select count(birthday) from student
-- 如果统计所有的数据,一般用主键列或*
select count(id) from student
select count(*) from student
注:
单独使用分组函数时没有问题
SELECT MAX(height) FROM student
如果使用分组函数的同时,还需要查询其他列名,则会报错,需要结合group by语句
SELECT no,name,MAX(height) FROM student
(1)条件查询
使用where 子句,将不满足条件的行进行过滤,where子句紧随from子句
语法:select<查询结果>from<表名>where<条件>
在where子句中使用的符号以及逻辑运算符:
比较: =,!=,>,<,>=,<= between and 两者之间,包含临界值;
逻辑运算:and 与
or 或
not 非
-- select 查询结果 from 表 where 条件 [排序,数量限制,分组 分组后条件筛选]
select * from student where gender ='男'
-- and 并且 与
select * from student where gender = '男' and height>=1.80 -- 有多个条件可以继续添加
-- or 或
select * from student where gender = '男' or height>=1.80
select * from student where height>=1.70 and height<=1.80
select * from student where height>1.70 and height<1.80
-- 在两个值之间,包含边界值
select * from student where height between 1.70 and 1.80
模糊查询
LIKE:是否匹配于一个模式 一般和通配符搭配使用,可以判断字符型数值或数值型.
通配符: 匹配% 任意多个字符
in 判断某字段的值是否属于in列表中的某一项
not in判断某字段的值是否不属于in列表中的某一项
IS NULL(为空的)
IS NOT NULL(不为空的)
(2)查询时的合并
1,UNION的语法如下
SQL语句1
UNION
SQL语句2
2,UNION ALL的语法如下
SQL语句1
UNION ALL
SQL语句2
当使用union 时,mysql 会把结果集中重复的记录删掉,而使用union all ,mysql 会把所有的记录返回,且效率高于union 。
-- UNION 合并时,可以去除多条语句查询出的重复数据
select num,name,gender from student where gender = '男'
union
select num,name,gender from student where gender = '女'
-- union all 只是简单的合并没有去重
select num,name,gender from student
union all
select num,name,gender from student where gender='女'
排序
1.查询结果排序
查询结果排序,使用order by 子句排序 order by 排序列 asc/desc
asc代表的是升序,desc代表的是降序,如果不写asc/desc 则默认是升序
order by 子句中可以支持单个字段,多个字段
-- 排序 order by 列名 asc (升序)/desc(降序)
select * from student where num>1 order by height asc
-- 先数据来源,在走条件,再走查询结果
select * from student order by height asc
select * from student order by regtime desc-- 注册日期降序
select * from student order by height desc,regtime asc -- 多个字段进行排序,相同位置再根据第二个排顺序,当身高相同时再根据regtime 升序进行排序
2.查询结果数量限制
limit子句:对查询的结果显示结果限制数量(sql 语句最末尾位置)
-- 数量限制 limit (开始的位置,每次查询的数量) 开始的位置从零开始
select * from student where num>1 order by id asc limit 0,2
select * from student order by id asc limit 2,2
select * from student limit 4,2 -- 放在sql语句最末尾位置
mysql分页公式 :limit (n-1)*每页大小,每页大小
根据第几页选择,其中n是第几页
分组查询
语法:
select 分组函数,列(字段) from 表 group by 分组的列 [having 分组后的筛选条件]
可以同时对两列进行分组
having是对查询后的结果进行处理
-- 用哪个列作为分组条件,会把该列相同的数据分到同一组处理
select gender,count(*) from student where num>1 group by gender
注:查询时语法运行顺序