Mysql 单表查询where初识
Mysql 单表查询where初识
准备数据
-- 创建测试库
-- drop database if exists student_db;
create database student_db charset=utf8;
use student_db;-- 创建测试表-学生表
create table students(id int unsigned primary key auto_increment not null,name varchar(20) default "",age tinyint unsigned default 0,height decimal(5,2),gender enum ("男", "女", "未填写") default "未填写",class_id int unsigned default 1,is_delete bit default 0
);-- 班级表
create table classes(id int unsigned auto_increment primary key not null,name varchar(20) not null
);-- 插入测试数据, 都是偶像, 没有其他意思哈.
insert into students values
(0,'爱因斯坦',18,180.00,1,1,0),
(0,'居里夫人',18,180.00,2,2,1),
(0,'小王子',14,185.00,1,1,0),
(0,'李银河',59,175.00,1,2,1),
(0,'黄蓉',38,160.00,2,1,0),
(0,'冰心',28,150.00,2,2,1),
(0,'王祖贤',18,172.00,2,1,1),
(0,'周杰伦',36,NULL,1,1,0),
(0,'王小波',57,181.00,1,2,0),
(0,'林徽因',25,166.00,2,2,0),
(0,'小星',33,162.00,3,3,1),
(0,'张爱玲',12,180.00,2,4,0),
(0,'冯唐',12,170.00,1,4,0),
(0,'胡适',34,176.00,2,5,0);insert into classes values
(0, "科学"),
(0, "艺术");-- 偶像查询-测试
mysql> select * from students;
+----+----------+-----+--------+--------+----------+-----------+
| id | name | age | height | gender | class_id | is_delete |
+----+----------+-----+--------+--------+----------+-----------+
| 1 | 爱因斯坦 | 18 | 180.00 | 男 | 1 | 0 |
| 2 | 居里夫人 | 18 | 180.00 | 女 | 2 | 1 |
| 3 | 小王子 | 14 | 185.00 | 男 | 1 | 0 |
| 4 | 李银河 | 59 | 175.00 | 男 | 2 | 1 |
| 5 | 黄蓉 | 38 | 160.00 | 女 | 1 | 0 |
| 6 | 冰心 | 28 | 150.00 | 女 | 2 | 1 |
| 7 | 王祖贤 | 18 | 172.00 | 女 | 1 | 1 |
| 8 | 周杰伦 | 36 | NULL | 男 | 1 | 0 |
| 9 | 王小波 | 57 | 181.00 | 男 | 2 | 0 |
| 10 | 林徽因 | 25 | 166.00 | 女 | 2 | 0 |
| 11 | 小星 | 33 | 162.00 | 未填写 | 3 | 1 |
| 12 | 张爱玲 | 12 | 180.00 | 女 | 4 | 0 |
| 13 | 冯唐 | 12 | 170.00 | 男 | 4 | 0 |
| 14 | 胡适 | 34 | 176.00 | 女 | 5 | 0 |
+----+----------+-----+--------+--------+----------+-----------+
14 rows in set (0.08 sec)mysql> select * from classes;
+----+------+
| id | name |
+----+------+
| 1 | 科学 |
| 2 | 艺术 |
+----+------+
2 rows in set (0.07 sec)
数据基本测试
-- 查询所有字段
select * from students limt 2;
+----+----------+-----+--------+--------+----------+-----------+
| id | name | age | height | gender | class_id | is_delete |
+----+----------+-----+--------+--------+----------+-----------+
| 1 | 爱因斯坦 | 18 | 180.00 | 男 | 1 | 0 |
| 2 | 居里夫人 | 18 | 180.00 | 女 | 2 | 1 |
+----+----------+-----+--------+--------+----------+-----------+
-- 查询指定字段
select name, age from students limit 2;
+----------+-----+
| name | age |
+----------+-----+
| 爱因斯坦 | 18 |
| 居里夫人 | 18 |
+----------+-----+
-- as 给查询集字段取别名
select name as "姓名", age as "年龄"
from students
where id in (1,2);+----------+------+
| 姓名 | 年龄 |
+----------+------+
| 爱因斯坦 | 18 |
| 居里夫人 | 18 |
+----------+------+-- as 给查询集表取别名
select s.name, s.age
from students as s
where s.gender = "女";+----------+-----+
| name | age |
+----------+-----+
| 居里夫人 | 18 |
| 黄蓉 | 38 |
| 冰心 | 28 |
| 王祖贤 | 18 |
| 林徽因 | 25 |
| 张爱玲 | 12 |
| 胡适 | 34 | -- 故意写错的
+----------+-----+-- 过滤重复行
select distinct gender
from students
+--------+
| gender |
+--------+
| 男 |
| 女 |
| 未填写 |
+--------+
where 条件过滤
比较运算符, 逻辑运算符, 范围判断, 空判断, 模糊查询
-- 比较运算符: <, <=, =, >, >=, !=-- 年龄小于20的信息
-- 年龄小于或等于20岁
select *
from students
where age <= 20; -- 年龄大于或等于20
select *
from students
where age >= 20;-- 年龄等于20
select * from students where age = 20;
-- 年龄不等于20
select *
from students
where age !=20;
逻辑运算符: and, or, not
-- 年龄在20-30间的学生信息
select *
from students
where (age >= 18) and (age <= 30);-- 30岁以下的女生
select *
from students
where (age < 30) and (gender = "女");-- or
-- 身高超过180 或者 年龄在25以上的 男生 姓名和班级
select name, class_id as "班级"
from students
where ((height > 180) or (age > 25)) and (gender = "男");+--------+------+
| name | 班级 |
+--------+------+
| 小王子 | 1 |
| 李银河 | 2 |
| 周杰伦 | 1 |
| 王小波 | 2 |-- not
-- 不在 20岁以上的女生姓名和身高
select name, height
from students
where not (age >= 20 and gender = "女");
Null 判断 is null; is not null
-- 身高为空值的人的姓名年龄和身高
select name, age, height
from students
where height is null;+--------+-----+--------+
| name | age | height |
+--------+-----+--------+
| 周杰伦 | 36 | NULL |
+--------+-----+--------+-- 非空 is not null
select name, age, height
from students
where height is not null;
范围查询 in; between...and
in 用于离散型, beween...and 用于连续型, 闭区间
-- in, 离散型: 年龄是18, 22, 24, 27 岁的女生姓名和身高
select s.name, s.height
from students s
where (age in (18, 22, 24, 27)) and (gender = "女");-- 不在, 即改为, not in 即可
+----------+-----+
| name | age |
+----------+-----+
| 居里夫人 | 18 |
| 王祖贤 | 18 |
+----------+-----+-- between..and.连续型: 年龄在18到35岁之间的女生姓名及身高
select s.name, s.height
from students as s
where (age between 18 and 35)and gender = "女";
+----------+--------+
| name | height |
+----------+--------+
| 居里夫人 | 180.00 |
| 冰心 | 150.00 |
| 王祖贤 | 172.00 |
| 林徽因 | 166.00 |-- 年龄不在在18到27之间的的女生姓名
select s.name
from students s
where (not (age between 18 and 27))and gender = "女";
模糊查询 like, regexp
就通配符和正则表达式两种形式, 关于正则表达式, 以后再写吧.
-- like
-- % 替换任意个; _ 替换1个, _ _ 替换2个;-- 姓名中,以"王"开头的所有名字及其年龄
select s.name, s.age
from students s
where name like "王%";
+--------+-----+
| name | age |
+--------+-----+
| 王祖贤 | 18 |
| 王小波 | 57 |
+--------+-----+
2 rows in set (0.06 sec)-- 姓名中, 带有"王"的所有名字
select s.name
from students s
where s.name like "%王%";
+--------+
| name |
+--------+
| 小王子 |
| 王祖贤 |
| 王小波 |
+--------+-- 姓名只有2个字的名字
select s.name
from students as s
where s.name like "__";
+------+
| name |
+------+
| 黄蓉 |
| 冰心 |
| 小星 |
| 冯唐 |
| 胡适 |
+------+
5 rows in set (0.05 sec)-- 姓名最至少有2个字的名字
select s.name
from students s
where s.name like "__%";-- 灵活: 正则表达式
-- 名字的第二个字是 "王"
select s.name
from students as s
where s.name regexp ".王.*";+--------+
| name |
+--------+
| 小王子 |
+--------+
1 row in set (0.11 sec)
正则表达式还有更加丰富而灵活的用法, 后面有空整吧, 后面再弄一波select的排序呀, group by ..聚合这些常用.
小结
熟悉库表操作及维护更新数据这些基本操作是必备的
查询字段: select 字段1, 字段2 .. from 表名
where 常用过滤, 常用在**比较运算符, 逻辑运算符, NULL判断, 模糊查询, 范围查询
比较运算符
- <, <=, =, >, >=, <> , !=
逻辑运算符
- and
- or
- not
- 优先级是 ( ) < not < 比较运算符 < and < or
Null判断
- is null
- is not null
范围查询
- in 表示离散型
- between .. and ... ; not (字段 between...and....)
模糊查询 like
- % 通配符
- _, __,
- %__%
- regexp 正则非常强大和灵活,而且各语言是通用的, 必须掌握
Mysql 单表查询where初识相关推荐
- 运维高级学习(三):MySQL单表查询作业
MySQL第三次作业 MySQL单表查询作业 素材如下: DROP TABLE IF EXISTS `course`; CREATE TABLE `course` ( `cs_id` int(11) ...
- MySQL单表查询基础卷(A)
MySQL单表查询基础卷(A) 第一章 数据准备 第二章 数据展示 第三章 题目说明 第四章 参考答案 第一章 数据准备 -- 创建数据库,指定字符集 utf8 CREATE DATABASE IF ...
- mysql 单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名WHERE 条件GROUP BY fieldHAVING 筛选ORDER BY fieldLIMIT 限制条数 二 关键字的执行优 ...
- 0x06 MySQL 单表查询
一 单表查询语法 SELECT 字段1,字段2... FROM 表名WHERE 条件GROUP BY fieldHAVING 筛选ORDER BY fieldLIMIT 限制条数 二 关键字执行优先级 ...
- mysql单表查询详解
文章目录 一.单表查询 1.1 创建数据库 1.2 单表查询 1.2.1 查询所有字段 1.2.2 使用通配符*查询所有字段 1.2.3 查询指定字段 1.2.4 去重distinct的使用 1.2. ...
- mysql单表查询实例_MySQL简单查询详解-单表查询
MySQL简单查询详解-单表查询 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询的执行路径 一条SQL查询语句的执行过程大致如下图所示: 1>.客户端和服务端通过my ...
- Mysql单表查询和多表查询
单表查询 一 单表查询的语法 #查询数据的本质:mysql会到你本地的硬盘上找到对应的文件,然后打开文件,按照你的查询条件来找出你需要的数据.下面是完整的一个单表查询的语法select * from, ...
- MySQL单表查询练习题
数据准备:install.bat @ECHO OFF ::未配置环境变量下 cd C:\Program Files\MySQL\MySQL Server 5.7\bin\ SET dbhost=127 ...
- MySQL单表查询例题
一.单表查询 CREATE TABLE `worker` ( `部门号` int(11) NOT NULL, `职工号` int(11) NOT NULL, `工作时间` date NOT NU ...
最新文章
- 计算机应用12班,《计算机应用基础》上机试卷A(10级机电8—12班)
- 使用netsh命令来管理IP安全策略(详细介绍)
- OpenCV向JNI层的参数转换
- r语言 悲观剪枝_《R语言编程—基于tidyverse》新书信息汇总
- mysql查询单个员工信息_PHP+MySQL实现模糊查询员工信息功能示例
- nginx反向代理docker registry报”blob upload unknown解决办法
- 耗时6个月,整理了30款免费高评分软件,完爆付费
- 无线通信(1)-无线通信链路结构
- BigGAN论文翻译与理解
- Qt5 解决QSlider的valueChanged槽函数中setValue导致一直回调的问题
- AMD显卡无法安装驱动
- html5动态加载图片和加载视频
- PADS-Logic学习笔记
- java互联网医院源码 智慧医院源码 图文问诊系统源码
- 生活简单案例,分析管理中的深奥道理
- Chromebook2013 由Fyde os 升级为Deepin v20.2.1 (一)
- MT5016A-ASEMI三相电机整流桥MT5016A
- 闪讯1.2.16.20、闪讯1.2.17.23开不了猎豹wifi的解决方法:
- 静脉血管穿刺存在的问题
- 山西高考成绩位次查询2021,2020年山西高考位次排名查询 山西高考位次所对应的学校...
热门文章
- BeanFactory 和 ApplicationContext的区别
- 瑞萨RA系列-开发环境搭建
- Spring 3.0 学习-环境搭建和三种形式访问
- 用户注册密码加密和后端校验的权衡
- ai-人工智能的本质和未来_人工智能的历史-从一开始
- 无线路由登不上服务器怎么办,无线路由器管理界面怎么登录,无法进入管理界面怎么办...
- Matlab笔记(二):Matlab实现高斯函数的三维显示
- opencv曝光过度_使用 OpenCV 进行曝光融合(Exposure Fusion)成像
- python是非常依赖于已编译完成的代码吗_初识Python的几点疑惑
- mysql开发二手书籍交易_基于PHP+MySQL二手书交易系统