SQL必知必会--基础篇(一)
最近又把《SQL 必知必会》仔细翻了一遍,因此将基础知识整理回顾,加深印象。
- sql 结构化查询语言(Structured Query Language 的缩写),用于访问和处理数据库;
- sql 不区分大小写,处理时空格被忽略;
- 多条语句必须以分号(;)分隔,建议每条语句末端都使用分号。
本篇包含知识点如图:
假设有两张表:student(sno,name,sex,age,class,addr) ,sno为主键
grade(id,sno,Math,Chinses,English),id为主键
以下sql语句,基于mysql数据库编写
一、检索数据(select)
select sno from student; # 检索单个列
select sno,sex,age from student; # 检索多列
select * from student; # 检索所有列 优点:能检索出名字未知的列 缺点:降低检索性能
检索唯一不同/不重复 值 : distinct
select distinct name from student;
注意:distinct 关键字,作用于所有列,而不仅仅是跟在其后的那一列。
下面的语句,因为指定的两列不完全相同,所以会返回student所有的行。
select distinct name,addr from student;
限制检索结果:limit
select name from student # 检索student表中从第 3 行起的 4 行数据
limit 4 offset 2; # 第一个被检索的是第0行,所以 2 实际是检索第 3 行
limit 指定返回的行数,offset 指定从哪里开始。
二、排序(order by)
升序(默认的):ASC 降序:DESC (order by 必须是select 的最后一条子句)
单个列排序
select name,age feom student
order by age; # 按年龄升序排列
多个列排序
select name,age,class from student
order by age,class; # 先按年龄排,年龄相同再按班级排
指定方向排序
select name,age,class from student
order by age DESC,class DESC; #若在多个列上降序,必须对每一列指定DESC关键字
三、过滤数据(where / and / or / not / is null / between / in )
where +条件
select name,age,class feom student
where age > 15; # 检索年龄大于15岁的
select name,age,class feom student
where age <= 18; # 检索年龄小于等于18的
select name,age,class feom student
where age = 14; # 检索年龄等于14的
select name,age,class feom student
where age != 12; # 检索年龄不等于12的
逻辑运算:and / or / not 优先级:( ) > not> and >or
select name,age,class feom student
where age = 10 and class = 14; # 同时满足两个条件的值 年龄=10 且 班级=14
select name,age,class feom student
where age = 15 or class = 16; # 满足其中任一条件的值 年龄=15 或者 班级=16
select name,age,class feom student
where not age = 15; # 不包含该条件的值 年龄!=15
特殊条件:is null / between / in
(1)NULL:无值,它与字段包含0,空字符串或仅仅包含空格不同;无法比较NULL和0,因为他们是不等价的。
判断是null值:is null 判断不是null值:is not null
select name,addr from student
where addr is null; # 地址是null值
select name,addr from student
where addr is not null; # 地址不是null值
(2)范围值检索 between and / or
select name,age from student
where age between 12 and 16; # 年龄在12~16之间
select name,age from student
where age in(10,12,14,16,18); # 年龄是括号中的值的
PS: in 与 or 能完成相似的功能,但 in 更好。
因为:in操作符的语法更清楚、直观;求值顺序更易于管理;执行速度相对or要快一些;可以包含其他select语句。
(3)like 模糊检索(% ,_ , [ ])
%:任何字符出现任意次数,匹配0个、1个或多个(不匹配null)
select name from student
where name like '%mark%'; # 匹配 name 包含 mark 的
select name from student
where name like '%mark'; # 匹配 name 以 mark 结尾的
select name from student
where name like 'mark%'; # 匹配 name 以 mark 开头的
_ : 匹配单个字符(总是刚好匹配一个字符,不能多也不能少)
select name from student
where name like '_ark'; # 匹配长度为4,且后三个字符为ark的,例如 mark/lark/hark
select name from student
where name like 'mar_'; # 匹配长度为4,且前三个字符为mar的,例如 mard/marl/maef
select name from student
where name like '_ma_'; # 匹配长度为4,且中间两个字符为ma的,例如 mmaa/smad/kmaf
[ ] :匹配括号中任意一个字符,只能匹配单个字符;可以用前缀字符^来否定
select name from student
where name like '[mk]%'; # 匹配以 m 或 k 开头的,例如 mark/kind/mind/kol
select name from student
where name like '%[mn]'; # 匹配以 m 或 n 结尾的,例如 moon/han
select name from student
where name like '[^abc]%'; # 匹配不以 a 或 b 或 c 开头的,例如 doop/lamb
PS:使用通配符的技巧
使用通配符检索,要比其他的检索花费更长的处理时间;能不用就不用,非要用就尽量不要放在搜索的开始处。把通配符放在开始处,搜索起来是最慢的。
SQL必知必会--基础篇(一)相关推荐
- SQL必知必会(一)SQL基础篇
SQL基础 1.SQL语言 2.SQL开发规范 3.DB.DBS 和 DBMS 的区别是什么 4.SQL执行顺序 1.oracle中执行顺序 2.MYSQL执行顺序 3.sql关键字执行顺序 5. I ...
- SQL必知必会-进阶篇[SQL学习笔记]
本篇博客是对于陈旸老师极客专栏"SQL 必知必会"进阶篇的笔记总结.需要学习资料可私信. 文章目录 第20课 数据库优化 第21课 数据库的设计范式都有哪些? 数据表的键都有哪些? ...
- 【SQL必知必会】002-基础篇:了解SQL:一门半衰期很长的语言
[SQL必知必会]002-基础篇:了解SQL:一门半衰期很长的语言 文章目录 [SQL必知必会]002-基础篇:了解SQL:一门半衰期很长的语言 一.概述 二.半衰期很长的 SQL 三.入门 SQL ...
- asp sql ip地址排序_SQL必知必会读书笔记,30分钟入门SQL!
点击上方SQL数据库开发,关注获取SQL视频教程 SQL专栏 SQL数据库基础知识汇总 SQL数据库高级知识汇总 来源:https://segmentfault.com/p/1210000011760 ...
- 程序员必知必会之blog篇
程序员必知必会之blog篇 网易广州 赖勇浩(http://blog.csdn.net/lanphaday) 本文最初发表于恋花蝶的博客(http://blog.csdn.net/lanp ...
- 《MySQL必知必会》所有SQL语句图表集合(可作为查询表使用)---已完结
本篇文章是对<MySQL必知必会>所有语句知识点的图表集合,适合快速查询遗忘的SQL语句. 本文的脉络结构,首先先给出<MySQL必知必会>的目录和本书中用到的数据表的UML图 ...
- mysql必学十大必会_MYSQL 学习(一)--启蒙篇《MYSQL必知必会》
MYSQL必知必会 一. DDL 数据定义语言 Data Definition Language 是指CREATE,ALTER和DROP语句. DDL允许添加/修改/删除包含数据的逻辑结构,或允许用户 ...
- GitHub#SQL#:SQL必知必会
https://github.com/CyC2018/Interview-Notebook 一.基础 二.创建表 三.修改表 四.插入 五.更新 六.删除 七.查询 八.排序 九.过滤 十.通配符 十 ...
- SQL Server必知必会
SQL Server必知必会 2009-10-27-17:57:57 Structure Query Language:SQL 结构化 查询 语言 数据库产品: ...
- 【SQL必知必会笔记(3)】SELECT语句的WHERE子句数据过滤操作
上个笔记主要介绍了利用SELECT语句检索单个/多个/所有列,并利用DISTINCT关键字检索具有唯一性的值.利用LIMIT/OFFSET子句限制结果:以及利用ORDER BY子句排序检索出的数据,主 ...
最新文章
- [C++]Call virtual member function in constructor or destructor
- 共识协议(6)PBFT详细步骤
- Python字符与字节新编
- 遍历 HashSet 的方法
- 机器学习算法 随机森林学习 之决策树
- JavaScript 概述
- python iot_使用EduBlocks,适用于Linux的i3窗口管理器,必读新闻通讯,CI / CD,敏捷,IoT等进行Python编程
- 6-2 错误与异常处理
- 基于项目管理的软件产品研发管理研究
- theadClasses设置Bootstrap Table表头样式
- dcp9030cdn定影_兄弟DCP-9030CDN打印驱动下载|兄弟Brother DCP-9030CDN一体打印机驱动官方下载 - 维维软件园...
- PlatformIO中arduino框架下stm32编程实现原理
- 找到解决办法了,特回来写总结,the import cannot be resolved问题可以通过以下方法解决
- 滴滴Booster移动APP质量优化框架 学习之旅
- 在计算机病毒组成结构中,计算机病毒的结构中有哪三种机制组成?
- 离散数学期末复习知识点(不挂科系列)
- pands 画图 调整大小_关于cad画图比例,图纸比例,打印比例问题详解
- linux中vim保存快捷键设置,Linux中vim操作快捷键非常简洁
- /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found问题解决方法
- P1873 砍树(二分查找模板)