MySql教程

MySQL 7.DQL - 数据查询

本文主要是介绍MySQL 7.DQL - 数据查询,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

DQL 数据查询

select */字段列表/函数 from 表名;
[where 检索条件]
[group by 字段列表 [having 检索条件]]
[order by 字段列表 asc/desc]
[limit 分页参数]
子句 功能
select * / 字段列表 / 函数 返回被查询的字段列或表达式
from 表名 选择被查询的表
where 检索条件(and、or、like、in、between and) 筛选、过滤数据
group by 分组字段 [having 检索条件] 根据指定字段对数据分组
having 检索条件 筛选、过滤分组结果
order by 排序字段 [desc / asc] 根据指定字段对数据排序
limit 分页参数 / 跳过参数,分页参数 分页查询数据行

1.基础查询

(1).查询所有数据行的所有字段列
select * form users;

(2).查询所有数据行的部分字段列
select id,name,pw from users;

2.Where 条件查询

  • 可以在 where 子句中指定任何条件
  • 可以使用 and 或 or 指定多个条件,两者同时使用时优先运算 and
  • like 子句使用 % 或 _ 模糊搜索时,性能消耗大,所以尽量不要使用 % 和_
  • where子句还可以用在 update 和 delete 语句中

and or

(1).查询值在某个区间的数据
select * from users where age < 22 and age > 23;

(2).查询值不在某个区间的数据
select * from users where age not between 22 and 23;

(3).查询同时满足多个条件的数据, and 和 or 同时使用时要注意运算顺序
select * from users where age = 22 or age = 24 and sex = '女';
-- age=24且sex='女',或age=24
select * from users where (age = 22 or age = 24) and sex = '女';
-- age=22或age=24,且sex='女'

like

(1).一个 _ 表示一个任意字符
select * from users where name like '__';
-- name为两个任意字符的数据

select * from users where name like '_y';
select * from users where name like 'l_';

select * from users where name like '__y';
select * from users where name like 'l__';

(2). % 表示任意个任意字符
select * from users where name like '%q%';
-- name中任意位置含有 q 的数据

select * from users where name like 'q%'/'%q';
-- 以 q 开头/结尾的数据

3.MySQL中的聚合函数

max(), min(), sum(), avg(), count()
聚合函数通常搭配分组进行数据的统计

-- 计算表中最大score,最小score,score总和,平均score,score项数,并分别命名
select max(score) as s_max,
       min(score) as s_min,
       sum(score) as s_sum,
       avg(score) as s_avg,
       count(score) as s_count from users;
       
select count(*) from users;
-- count(*) 按照表中所有列进行统计,只要其中一列有数据,就会统计
-- count(字段) 按照指定字段进行统计,如果列中出现 null ,则这个数据不会被统计

4.Group by 分组

  • group by 语句根据一列或者多列字段对数据进行分组
  • 通常配合聚合函数使用,可以对分组后的结果统计、求和、求平均等
  • 使用 group by 时,除了聚合函数,其他在 select 后面出现的字段都需要写在 group by 后面
(1).根据两个字段对数据分组后,统计各组的总数
select classid,sex,count(*) as num from users group by classid,sex;
-- 根据classid、sex字段对数据进行分组,然后使用 count(*) 统计各组的总数

(2).根据一个字段对数据分组后,求各组中另一个字段的平均值
select classid,avg(age) as AVGage from class group by classid;
 -- 根据classid字段对数据进行分组,然后使用 avg(age) 求各组中age的平均值

(3).根据两个个字段对数据分组后,先求各组中另一个字段的平均值,再对平均值的结果进行筛选
select age,sex,avg(score) as AVGscore from class group by age,sex having avg(score) >= 75;
-- 根据age、sex字段对数据进行分组,然后使用 avg(score) 求各组中score的平均值,最后筛选出平均值在75及以上的组

5.order by 排序

  • 在MySQL中使用select语句查询的结果是根据数据在底层文件中的结构来排序的
  • 默认使用 asc ,即升序排列,使用 desc 为倒序排列
(1).根据多个字段对数据进行排序
select * from class order by classid,age desc,score desc;
-- 先根据classid升序排列,classid相同时根据age降序排列,age相同时根据score降序排列

6.limit 分页

  • limit 语句运用在数据分页,或查询排序后值为最大/最小的数据
  • limit n : 查询表中前 n 条数据
  • limit m,n : 跳过前 m 条数据,再查询 n 条数据
(1).查询表中前 n 条数据
select * from class limit 3;

(2).在表中跳过前 m 条数据,再查询 n 条数据
select * from class limit 5,3;

(3).查询表中某个字段值最大的 n 条数据
select * from class order by score desc limit 5;

这篇关于MySQL 7.DQL - 数据查询的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!