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:使用通配符的技巧

使用通配符检索,要比其他的检索花费更长的处理时间;能不用就不用,非要用就尽量不要放在搜索的开始处。把通配符放在开始处,搜索起来是最慢的。

 

 

  • 9
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值