MySQL 数据库基础:字段判空、逻辑查找、排序、限制、模糊搜索与聚合函数全解析

MySQL 是后端开发与数据处理最常用的数据库之一,掌握基本的查询语句,是写好程序的第一步。本篇文章带你从最基础的字段判空查找、逻辑查询,到排序、结果限制、模糊搜索,再到聚合函数,一次性掌握最常用的 SQL 语法。

文末还附带完整作业示例代码,可直接复制练习。


🧩 一、字段判空查询(IS NULL / IS NOT NULL)

当我们想知道某个字段是否有值时,可以使用:

✔ 查找字段为空的数据

sql 复制代码
select * from 表名 where 字段 is null;

示例:

sql 复制代码
select * from hk where username is null;

✔ 查找字段不为空的数据

sql 复制代码
select * from 表名 where 字段 is not null;

示例:

sql 复制代码
select * from hk where id is not null;

🧠 二、逻辑查询(AND / OR / NOT)

1) AND ------ 两个条件同时满足

sql 复制代码
select * from hk where age > 22 and age < 28;

2) OR ------ 满足其中一个即可

sql 复制代码
select * from hk where age > 22 or age < 28;

3) NOT ------ 条件取反

sql 复制代码
select * from hk where not age > 18;

🔽 三、排序(ORDER BY)

✔ 升序(默认)

sql 复制代码
select * from hk order by age;

✔ 降序(desc)

sql 复制代码
select * from hk order by age desc;

🎯 四、限制输出条数(LIMIT)

只取前 N 条:

sql 复制代码
select * from hk limit 3;

配合排序完成"取最大 / 最小":

sql 复制代码
select * from hk order by age desc limit 3;

🌀 五、去重(DISTINCT)

当你不希望某个字段重复出现时:

sql 复制代码
select distinct 字段 from 表名;

🔍 六、模糊匹配(LIKE)

使用 %关键字% 进行模糊查找:

sql 复制代码
select * from hk where username like '%wang%';

📏 七、范围查询(BETWEEN)

用于判断字段是否落在某个区间:

sql 复制代码
select * from hk where age between 20 and 25;

📊 八、聚合函数(COUNT / SUM / AVG)

这些函数常用于统计类需求:

✔ count() ------ 统计数量

sql 复制代码
select count(*) from hk;
select count(username) from hk;  -- 不统计为 null 的行

✔ sum() ------ 求和

sql 复制代码
select sum(age) from hk;

✔ avg() ------ 平均值

sql 复制代码
select avg(age) from hk;

📝 九、综合作业(完整 SQL 已给)

根据上面的知识点,下面是一个完整的练习题与参考答案:


🎯 作业要求

1️⃣ 创建库 yy

2️⃣ 创建用户表 haha,字段:id、username、age

3️⃣ 插入 10 条数据

4️⃣ 查找 username 为空的数据

5️⃣ 计算 age 字段的总和与平均值

6️⃣ 查找 age 在 16--18 岁之间的人

7️⃣ 模糊查找名字中含有 h 的用户


✅ 完整 SQL 示例代码(可直接复制运行)

sql 复制代码
-- 1. 创建库
create database yy;

-- 2. 使用数据库
use yy;

-- 3. 创建表 haha
create table haha(
    id int,
    username varchar(50),
    age int
);

-- 4. 插入 10 条数据
insert into haha values
(1, 'han', 18),
(2, 'li', 20),
(3, 'hong', 17),
(4, null, 19),
(5, 'hao', 16),
(6, 'zhang', 18),
(7, 'hua', 21),
(8, null, 22),
(9, 'he', 17),
(10, 'cheng', 23);

-- 5. 查找 username 为空的数据
select * from haha where username is null;

-- 6. 求 age 的和、平均值
select sum(age) from haha;
select avg(age) from haha;

-- 7. 查找 age 在 16 - 18 岁之间的人
select * from haha where age between 16 and 18;

-- 8. 模糊查询名字含有 h 的
select * from haha where username like '%h%';

🧩 十、总结

这篇文章系统地覆盖了 MySQL 中最常用的查询方式,包括:

  • 字段判空
  • 组合逻辑查询
  • 排序、限制
  • 去重与模糊查询
  • 范围与聚合函数

这些都是数据库初学者必须掌握的语法,也是实际开发中最常用的部分。熟练掌握这些基础,将为后续更深入的 SQL 查询和业务数据处理打下扎实基础。

相关推荐
高溪流1 小时前
3.数据库表的基本操作
数据库·mysql
alonewolf_991 小时前
深入剖析MySQL锁机制与MVCC原理:高并发场景下的数据库核心优化
数据库·mysql
一 乐1 小时前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
黄宝康2 小时前
sqlyog密钥亲测有效
mysql
Codeking__2 小时前
Redis初识——什么是Redis
数据库·redis·mybatis
YIN_尹2 小时前
【MySQL】数据类型(上)
android·mysql·adb
k***1952 小时前
Spring 核心技术解析【纯干货版】- Ⅶ:Spring 切面编程模块 Spring-Instrument 模块精讲
前端·数据库·spring
程序员黄老师2 小时前
主流向量数据库全面解析
数据库·大模型·向量·rag
Full Stack Developme2 小时前
Redis 可以实现哪些业务功能
数据库·redis·缓存
rgeshfgreh2 小时前
Spring事务传播机制深度解析
java·前端·数据库