MySQL - 07条件查询(单条件、多条件)

本文深入解析SQL查询语句中的where子句应用,包括单条件和多条件查询,通过实战案例展示如何使用and、or运算符进行高效数据筛选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

where子句(单条件查询)

在SQL中,insert、update、delete和select后面都能带where子句,用于插入、修改、删除或查 询指定条件的记录。

#SQL语句中使用where子句语法
SELECT column_name FROM table_name WHERE column_name 运算符 value
运算符描述
=等于
<> 或 !=不等于
>大于
<小于
>=大于等于
<=小于等于
between and选取介于两个值之间的数据范围;在MySQL中,相当于>=并且<=

where子句(多条件查询)

在where子句中,使用and、or可以把两个或多个过滤条件结合起来。

# and、or运算符语法
SELECT column_name FROM table_name WHERE condition1 AND condition2 OR condition3
运算符描述
and表示左右两边的条件同时成立
or表示左右两边只要有一个条件成立

实战案例

将下面的内容在mysql中执行

create table employee(
	id int not null auto_increment primary key,
	name varchar(30),
	sex varchar(1),
	salary int
);

insert into employee(name, sex, salary) values('lily0', '1', 5500);
insert into employee(name, sex, salary) values('lily1', '0', 4500);
insert into employee(name, sex, salary) values('lily2', '0', 4200);
insert into employee(name, sex, salary) values('lily3', '1', 7500);
insert into employee(name, sex, salary) values('lily4', '0', 8500);
insert into employee(name, sex, salary) values('lily5', '1', 6800);
insert into employee(name, sex, salary) values('lily6', '1', 12000);
insert into employee(name, sex, salary) values('lily7', '1', 3500);
insert into employee(name, sex, salary) values('lily8', '1', 6000);
insert into employee(name, sex, salary) values('lily9', '1', 8000);
insert into employee(name, sex, salary) values('lily10', '0', 10000);
insert into employee(name, sex, salary) values('lily11', '0', 4000);


mysql> select * from employee;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
|  1 | lily0  | 1    |   5500 |
|  2 | lily1  | 0    |   4500 |
|  3 | lily2  | 0    |   4200 |
|  4 | lily3  | 1    |   7500 |
|  5 | lily4  | 0    |   8500 |
|  6 | lily5  | 1    |   6800 |
|  7 | lily6  | 1    |  12000 |
|  8 | lily7  | 1    |   3500 |
|  9 | lily8  | 1    |   6000 |
| 10 | lily9  | 1    |   8000 |
| 11 | lily10 | 0    |  10000 |
| 12 | lily11 | 0    |   4000 |
+----+--------+------+--------+
12 rows in set (0.00 sec)

mysql> select * from employee where sex = '1';
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  1 | lily0 | 1    |   5500 |
|  4 | lily3 | 1    |   7500 |
|  6 | lily5 | 1    |   6800 |
|  7 | lily6 | 1    |  12000 |
|  8 | lily7 | 1    |   3500 |
|  9 | lily8 | 1    |   6000 |
| 10 | lily9 | 1    |   8000 |
+----+-------+------+--------+
7 rows in set (0.01 sec)

mysql> select * from employee where sex != '0';
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  1 | lily0 | 1    |   5500 |
|  4 | lily3 | 1    |   7500 |
|  6 | lily5 | 1    |   6800 |
|  7 | lily6 | 1    |  12000 |
|  8 | lily7 | 1    |   3500 |
|  9 | lily8 | 1    |   6000 |
| 10 | lily9 | 1    |   8000 |
+----+-------+------+--------+
7 rows in set (0.01 sec)

mysql> select * from employee where sex <> '0';
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  1 | lily0 | 1    |   5500 |
|  4 | lily3 | 1    |   7500 |
|  6 | lily5 | 1    |   6800 |
|  7 | lily6 | 1    |  12000 |
|  8 | lily7 | 1    |   3500 |
|  9 | lily8 | 1    |   6000 |
| 10 | lily9 | 1    |   8000 |
+----+-------+------+--------+
7 rows in set (0.00 sec)

mysql> select * from employee where salary > 8000;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
|  5 | lily4  | 0    |   8500 |
|  7 | lily6  | 1    |  12000 |
| 11 | lily10 | 0    |  10000 |
+----+--------+------+--------+
3 rows in set (0.00 sec)

mysql> select * from employee where salary between 10000 and 12000;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
|  7 | lily6  | 1    |  12000 |
| 11 | lily10 | 0    |  10000 |
+----+--------+------+--------+
2 rows in set (0.00 sec)

mysql> select * from employee where sex = '1' and salary > 10000;
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  7 | lily6 | 1    |  12000 |
+----+-------+------+--------+
1 row in set (0.00 sec)

mysql> select * from employee where sex = '1' or salary > 10000;
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  1 | lily0 | 1    |   5500 |
|  4 | lily3 | 1    |   7500 |
|  6 | lily5 | 1    |   6800 |
|  7 | lily6 | 1    |  12000 |
|  8 | lily7 | 1    |   3500 |
|  9 | lily8 | 1    |   6000 |
| 10 | lily9 | 1    |   8000 |
+----+-------+------+--------+
7 rows in set (0.00 sec)

mysql> select * from employee where sex = '1' or salary >= 10000;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
|  1 | lily0  | 1    |   5500 |
|  4 | lily3  | 1    |   7500 |
|  6 | lily5  | 1    |   6800 |
|  7 | lily6  | 1    |  12000 |
|  8 | lily7  | 1    |   3500 |
|  9 | lily8  | 1    |   6000 |
| 10 | lily9  | 1    |   8000 |
| 11 | lily10 | 0    |  10000 |
+----+--------+------+--------+
8 rows in set (0.00 sec)

mysql> select * from employee where sex = '1' and salary <= 4000 or salary >= 10000;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
|  7 | lily6  | 1    |  12000 |
|  8 | lily7  | 1    |   3500 |
| 11 | lily10 | 0    |  10000 |
+----+--------+------+--------+
3 rows in set (0.00 sec)

mysql> select * from employee where sex = '1' and (salary <= 4000 or salary >= 10000);
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  7 | lily6 | 1    |  12000 |
|  8 | lily7 | 1    |   3500 |
+----+-------+------+--------+
2 rows in set (0.00 sec)

mysql> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小沈曰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值