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初识的更多相关文章

  1. Mysql 单表查询-排序-分页-group by初识

    Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...

  2. Mysql 单表查询 子查询 关联查询

    数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...

  3. python 3 mysql 单表查询

    python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...

  4. python mysql 单表查询 多表查询

    一.外键 变种: 三种关系: 多对一 站在左表的角度: (1)一个员工 能不能在 多个部门? 不成立 (2)多个员工 能不能在 一个部门? 成立 只要有一个条件成立:多 对 一或者是1对多 如果两个条 ...

  5. MySQL单表查询

    MySQL之单表查询 创建表 # 创建表 mysql> create table company.employee5( id int primary key AUTO_INCREMENT not ...

  6. mysql 单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数   二 ...

  7. SQL学习笔记四(补充-1)之MySQL单表查询

    阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录 ...

  8. python开发mysql:单表查询&多表查询

    一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...

  9. 0x06 MySQL 单表查询

    一 单表查询语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键字 ...

随机推荐

  1. Windows上的那些效率神器,让你快到飞起

    转自:https://zhuanlan.zhihu.com/p/41771626 本文为Windows上有哪些让你效率倍增的软件和小技巧系列第二篇,上一篇请点击蓝字查看:打造高逼格PC,让你的电脑好用 ...

  2. java中map和对象互转工具类的实现示例

    在项目开发中,经常碰到map转实体对象或者对象转map的场景,工作中,很多时候我们可能比较喜欢使用第三方jar包的API对他们进行转化,而且用起来也还算方便,比如像fastJson就可以轻松实现map ...

  3. ubuntu连接多个realsense d435

    ubuntu连接多个realsense d435 import pyrealsense2 as rs import numpy as np import cv2 import time import ...

  4. python万能消费框架,新增7种中间件(或操作mq的包)和三种并发模式。

    新增的中间件和并发模式见注释. 消息队列中间件方面celery支持的,都要支持.并发模式,celery支持的都要支持. 从无限重复相似代码抽取框架,做成万能复用,是生产力的保障. 使用模板模式使加新中 ...

  5. 安装 python 爬虫框架 Scrapy

    官方安装说明文档:https://doc.scrapy.org/en/latest/intro/install.html#installing-scrapy 一.scrapy 需要以下依赖 二.一般来 ...

  6. 数学黑洞:卡普雷卡尔常数的php算法实现

    首先看一篇文章: 英国广播公司报道,6174乍看没什么奇特之处,但是,自从1949年以来,它一直令数学家.数字控抓狂.痴迷. 不管你挑的四位数是什么,早早晚晚你都会遇到6174:而且,遇到6174就只 ...

  7. excel 去掉 空单元格

    Excel 2003 选中这一列,定位(CTRL+G)--定位条件--空值--确定--右键--删除. 1. 然后进行全选已输入的内容,可以使用鼠标拖动已输入的内容,也可以使用快捷键全选内容,按住ctr ...

  8. .NET Core开源Quartz.Net作业调度框架实战演练

    一.需求背景 人生苦短,我用.NET Core!作为一枚后端.NET开发人员,项目实践常遇到定时Job任务的工作,在Windows平台最容易想到的的思路Windows Service服务应用程序,而在 ...

  9. SpringBoot系列教程web篇之404、500异常页面配置

    接着前面几篇web处理请求的博文,本文将说明,当出现异常的场景下,如404请求url不存在,,403无权,500服务器异常时,我们可以如何处理 原文友链: SpringBoot系列教程web篇之404 ...

  10. DDD分层架构的三种模式

    引言 在讨论DDD分层架构的模式之前,我们先一起回顾一下DDD和分层架构的相关知识. DDD DDD(Domain Driven Design,领域驱动设计)作为一种软件开发方法,它可以帮助我们设计高 ...