数据库表查询进阶(1)

数据库表查询进阶(1),第1张

in *** 作符允许我们在 WHERE 子句中规定多个值。

where 字段名 in (值1,值2,值3....)

示例: where age in (20,21,25)

age 字段的值 只要是 20或者21或者25 的数据 都满足条件

where not in (值1,值2,值3....)</pre>

示例: where age in (20,21,25)

age 字段的值 只要 不是 20或者21或者25 的数据 都满足条件

select *

from sc

where score in (70,80,90)

select *

from sc

where score not in (70,80,90)

select *

from student

where birthday in ('1990-01-01','1990-12-21','2017-12-20','2012-06-06')</pre>

select *

from student

where birthday not in ('1990-01-01','1990-12-21','2017-12-20','2012-06-06')

MySQL中的分页查询, limit 通常放在sql 语句的最末尾

limit 4

查询前4条数据

limit 4,3

从第4条数据之后开始,查询3条数据

也就是 第5,6,7条的数据

limit 4 offset 10

offset后面的数字是指记录数

从第10条 之后的数据开始 查询4条数据

也就是 第 11,12,13,14条数据

select * from student limit 5

select * from student limit 9,6

select * from student limit 6 offset 9

查询成绩表中 前7条的数据;

select * from sc limit 7

查询成绩表中 第 5到10 条的数据

select * from sc limit 4,6

查询成绩表中 第 3到8 条的数据

select * from sc limit 2 , 6

如果字段没有要求非空,则字段值 可以为空,就是null值。如果要查询某个 字段 为 null的数据,不能使用 字段名=null,要用 字段名 is null;

where 字段名 is null;

字段名 为空的符合条件

where 字段名 is not null;

字段名 不为空的符合条件

1.查询teacher表,给tid 起别名为 教师编号,tname 起别名为 教师姓名

select tid as 教师编号,tname as 教师姓名 from teacher

2.查询 sc 表, 给 cid 起别名为 课程编号,sid 起别名为 学生编号, score起别名为分数;

select cid 课程编号 ,sid 学生编号,score 分数 from sc

3.查询 course 表,给表起别名为 c,使用 c.字段查询 表数据

select c.* from course as c

起别名是为了后续学习多表联查 做铺垫,语法十分简单

比如查询 student 表的 学生姓名 和 生日 的sql语句:

select sname,birthday from student

给 sname,birthday字段 起别名 ,改为中文

select sname as 姓名,birthday as 生日 from student

起了别名后,查出来的字段会以别名展示。

as 关键字也可以省略,字段名 和 别名之间 用空格隔开即可

select sname 姓名, birthday 生日 from student

例子:

select a.sname, a.birthday from student as a

给表 起别名 同样是 使用 as关键字(可以使用空格隔开代替as关键字), a 就代表 student表,查询字段时,可以 使用 a.sname 代表 student的sname字段。

给表起别名 通常用在 多表查询,本次使用演示,先熟悉语法。

在某些复杂的场景下,可能需要多条sql 才能查询出想要的数据,把多条sql拼接起来 就是嵌套查询

句式1

select XXX from A表 where xx =/in (select XXX from B表)

句式2

select XXX from (select XXX from XXXX)b

句式3

select XXX from A表 a

join (select XXX from B表)b on a.xx = b.xx

select * from sc where sid = (select sid from student where sname = '周梅')

select * from sc where sid in (select sid from student where sex='女')

select a.sname,b.score,c.score from student a

inner join (select sid,score from sc where cid =1) b on a.sid = b.sid

inner join (select sid,score from sc where cid = 2) c on a.sid = c.sid

where b.score › c.score

聚合函数对一组值执行计算,并返回单个值,也被称为组函数。

就是sql提供的一种查询方法,比如查询 最大值,最小值,总数,平均等等。

聚合函数不能写在where的查询条件里面

查询 最小值

select min(字段) from 表;

查询 最大值

select max(字段) from 表;

select max(tid) from teacher

select min(tid) from teacher

select max(a.score) ,b.cname from sc a

inner join course b on a.cid=b.cid

where a.sid =(select sid from student where sname='周梅') group by b.cname limit 1

查询 该字段的 总数

select sum(字段) from 表

查询 该字段的 平均数

select avg(字段) from 表

select avg(score) from sc

select avg(s.score) from student a inner join sc s on a.sid = s.sid

where a.sex ='女'

查询 数据 总条数

count( ) 是查询总共有多少条数据

count(字段) 是查询该字段 不为 null的 总共有多少条数据

例:

select count( ) from 表

去重,查询数据时,相同的数据只展示 一条

语法:

select distinct 字段 from 表

例:去重查询学生表中的生日字段

select distinct birthday from student

一、数据准备

以Microsoft SQL Server数据库管理系统为例。

假设对应的数据表名称为TestTable,表结构如下所示:

CREATE TABLE TestTable

(

[品牌] VARCHAR(20),

[版本号] INT,

[颜色] VARCHAR(10)

)

添加样例数据的SQL代码如下:

INSERT INTO TestTable VALUES('三星', 1, '黑')

INSERT INTO TestTable VALUES('三星', 1, '白')

INSERT INTO TestTable VALUES('三星', 1, '灰')

INSERT INTO TestTable VALUES('三星', 1, '蓝')

INSERT INTO TestTable VALUES('三星', 1, '红')

INSERT INTO TestTable VALUES('苹果', 1, '黑')

INSERT INTO TestTable VALUES('苹果', 1, '白')

INSERT INTO TestTable VALUES('HTC', 1, '黑')

INSERT INTO TestTable VALUES('HTC', 1, '白')

INSERT INTO TestTable VALUES('HTC', 1, '灰')

INSERT INTO TestTable VALUES('HTC', 1, '蓝')

INSERT INTO TestTable VALUES('HTC', 1, '红')

INSERT INTO TestTable VALUES('三星', 2, '黑')

INSERT INTO TestTable VALUES('三星', 2, '白')

INSERT INTO TestTable VALUES('HTC', 2, '黑')

INSERT INTO TestTable VALUES('HTC', 2, '白')

INSERT INTO TestTable VALUES('HTC', 3, '黑')

INSERT INTO TestTable VALUES('HTC', 3, '白')

INSERT INTO TestTable VALUES('HTC', 3, '灰')

INSERT INTO TestTable VALUES('HTC', 3, '蓝')

INSERT INTO TestTable VALUES('HTC', 3, '红')

二、思路

查询所有数据行,对结果集按“品牌”分组,比较分组后的每行的版本号是否是同一品牌的最新版本号,若不是则从结果集中剔除。

GROUP BY    分组列

HAVING      分组后的条件子句

三、实现步骤

完整的SQL代码如下:

SELECT t1.[品牌], t1.[版本号], t1.[颜色]

FROM TestTable t1

GROUP BY t1.[品牌], t1.[版本号], t1.[颜色]

HAVING t1.[版本号] = ( SELECT MAX(t2.[版本号])

FROM TestTable t2

WHERE t1.[品牌] = t2.[品牌])

四、运行测试

运行结果:

品牌                   版本号         颜色

-------------------- ----------- ----------

苹果                   1           白

苹果                   1           黑

三星                   2           白

三星                   2           黑

HTC                  3           白

HTC                  3           黑

HTC                  3           红

HTC                  3           灰

HTC                  3           蓝

(9 行受影响)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6425698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-22
下一篇 2023-03-22

发表评论

登录后才能评论

评论列表(0条)

保存