当前位置: 首页 > news >正文

SELECT的使用

目录

1、SQL的查询命令

1.1 SELECT语句

1.2 投影查询

1.3 选择查询

1.4 聚合函数查询

1.5 简单分组查询(GROUP BY)

1.6 内连接查询

1.7 外连接查询

1.8 子查询

1. 无关子查询

2. 相关子查询

带exists的相关子查询:

1.9 集合查询

1. UNION(并)

2. INTERSECT(交)和EXCEPT(差)

1.10 排序(ORDER BY)

1.11 存储查询结果

2、数据操作中使用SELECT字句

2.1 INSERT中使用SELECT字句

2.2 UPDATE中使用SELECT字句

2.1 DELETE中使用SELECT字句


T-SQL的语法约定

数据库的概念和操作-CSDN博客

接下来查询,我们用,在teaching数据库里的,三张表:

student(sno(主码),sname,gender,sage,specialty(专业),grade(年级)),

course(cno(主码),cname,credit),

sc(sno(外码),cno(外码),score),(sno,cno)为主码。

1、SQL的查询命令

1.1 SELECT语句

SELECT [ALL|DISTINCT] <expression>[,...n]
FROM <table|view>[,...n]
[WHERE <condition_expression>]
[GROUP BY <column_name>[,...n] [HAVING <group_condition_expression>]
[ORDER BY <column_name> [ASC|DESC] [,...n]]

SELECT返回的是一个关系

ALL|DISTINCT默认ALL不去重DISTINCT去重一个就指定所有的列

expression表达式,通常取列名,所有列可用 * 表示。

FROM:从一个或多个表(视图)中查询。

WHEREcondition_expression可以是

1. 比较运算符:=、>、<、>=、<=、<>(!=)。

2. 逻辑运算符:AND(与)、OR(或)、NOT(非)。

3. 确定范围运算符:BETWEEN AND,NOT BETWEEN AND,可以用比较运算符代替。

4. 集合成员运算符:IN、NOT IN。

5. 字符匹配运算符:LIKE,NOT LIKE。

6. 判空运算符:IS NULL、IS NOT NULL。

7. 聚合函数:AVG()、MAX()、MIN()、SUM()、COUNT()。

8. SELECT 字句:SELECT可以嵌套。

9. 谓词:IN、EXISTS、ALL、SOME、ANY。

10. 集合运算符:UNION(并)、INTERSECT(交)、EXCEPT(差)。

GROUP BY:按一个或多个列,分组。

HAVING:在GROUP BY中可选(没有GROUP BY,不能单独写HAVING),给分组加上条件

ORDER BY:对最终的查询结果进行排序默认ASC升序,DESC降序。

1.2 投影查询

查询stduent表中的sname,gender,specialty。

use teaching
go
select sname,gender,specialty
from student

查询student表所有列。

select * from student

查询student表前三行(包括所有列)。

select top 3 *
from student

查询student表前50%(包括所有列)。

select top 50 percent *
from student

给查询到的列改名

select sno as 学号,sname as 名字 -- as 可省略,但可读性差
from student
-- 或
select 学号=sno,名字=sname -- =是赋值,不能反了
from student

当然,也可以 select 表达式,如:

select sno,sage+10 as 十年后的年龄
from student

1.3 选择查询

关系运算符

查询sc表中不及格的sno

select sno
from sc
where score<60

逻辑运算符

查询计算机中的男生的信息

select *
from student
where specialty = '计算机' and gender = '男'

IN关键字

查询计算机或电子商务专业的学生信息。

select *
from student
where specialty in ('计算机','电子商务')
-- 也可以这样写 where specialty = '计算机' or specialty = '电子商务'

LIKE关键字

LIKE '%',0~N任意字符

LIKE ‘_’,任意字符

LIKE '[ ]',范围中任意字符

LIKE '[^ ]',不在范围中任意字符

查询姓张的同学的信息。

select *
from student
where sname like '张%'

判空运算符

查询score是NULL的学生信息。

select *
from sc
where score is NULL

1.4 聚合函数查询

count(*):返回元组个数

count(column_name):返回该列属性值的个数(不去重)。

count(distinct column_name):返回该列属性值的个数(去重)。

avg(column_name):返回该列的平均值

sum(column_name):返回该列的总和

max(column_name):返回该列的最大值

min(column_name):返回该列的最小值

注意:返回的值,没有列名,自己取名字。

注意:where不能使用聚合函数

如:

查询专业的个数。

select count(distinct specialty) as 专业个数
from student

1.5 简单分组查询(GROUP BY)

group by 后的表达式,要与select中的表达式匹配

group by 后多个表达式,先按第一个分组,再按第二个分组……

HAVING 子句用于在分组(GROUP BY)查询之后进行筛选。

查询男生,女生的人数。

select gender,count(gender) as 人数
from student
group by gender

group by 后面有gender,select 后面也要有gender。

查询一个学生的总成绩,平均成绩。

use teaching
go
select sno,sum(score) as 总分,avg(score) as 平均成绩
from sc
group by sno

 group by 后面有sno,select 后面也要有sno。

1.6 内连接查询

连接格式

SELECT select_list
FROM table1 INNER INTO table2 ON condition_expression

也可以select  select_list  from table1,table2  where condition_expression,但较旧或不推荐。   

一般连接条件是,使用外键连接

如:

查询sname,cno,score,

select student.sname,sc.cno,sc.score
from student inner join sc on student.sno = sc.sno

1.7 外连接查询

把INNER INTO换成

LEFT OUTER JOIN:以左表为主(),右表不满足补NULL。

RIGHT OUTER JOIN:以右表为主(),右表不满足补NULL。

FULL OUTER JOIN :左,右表不满足时,各自对应位置补NULL。

其他不变。

1.8 子查询

1. 无关子查询

子查询与外部查询无关子查询只执行一次

查询与刘珊的专业相同的同学的sno,snsme,specialty

use teaching
go
select sno,sname,specialty
from student
where specialty = (
select specialty
from student 
where sname = '刘珊') and sname != '刘珊'

当然,也可以使用自连接:

use teaching
go
select a.sno,a.sname,a.specialty
from student as a inner join student as b on a.specialty = b.specialty
where b.sname = '刘珊' and a.sname != '刘珊'

查询选修了cno为‘0807’的学生的sname和specialty。

select sname,specialty
from student as s inner join sc on s.sno = sc.sno
where sc.cno = '0807'
2. 相关子查询

子查询依赖于外部查询。子查询重复执行

cah查询成绩比该课平均成绩低的学生的sno,cno,score

select sno,cno,score
from sc as a
where score<(
select avg(score)
from sc as b
where a.cno = b.cno)

可以认为是双重for循环

先确定a表一行的cno的值,1

把b表中所有等于这值的cno(相当于选了这个课程)的这些元组中的score求平均值,2

a表中该行的score与score平均值比较,留下符合条件的元组,3

直到a表中的每一行都执行一遍1,2,3,

最后在符合条件的元组中进行外部的查询。

exists的相关子查询

EXISTS

子查询的结果集有元组(有满足的元组),返回TRUE,外层查询的某一行要被留下

若子查询的结果集中没有元组(没有满足的元组),返回FALSE,外层查询的某一行要被删除。

NOT EXISTS

子查询的结果集中没有元组(没有满足的元组),返回TRUE,外层查询的某一行要被留下。

若子查询的结果集有元组(有满足的元组),返回FALSE,外层查询的某一行要被删除,

注意:因为子查询返回的是TRUE或FALSE,所以子查询里是,select *,只关心有没有元组

查询所有选修了‘0202’课程的学生的姓名。

select sname
from student
where exists(
select *
from sc
where student.sno = sc.sno and sc.cno = '0202')
-- 或
select student.sname
from student inner join sc on student.sno = sc.sno
where sc.cno = '0202'
-- 或
select student.sname
from student
where sno in (
select sno
from sc
where cno = '0202')

双循环思想:

先确定student表一行的sno,

因为是exists,所以有满足子查询的条件的元组,返回TRUE,外层查询的某一行要被留下

没有满足的元组,返回FALSE,外层查询的某一行要被删除

直到确定完所有student表中的元组时,

再对留下的元组进行外部查询。

查询没有选课的student学号、姓名、性别,

use teaching
go
select sno,sname,gender
from student as s
where not exists(
select *
from sc
where s.sno = sc.sno
)

双循环思想: 

先确定student表一行的sno,

因为是not exists,所以没有满足子查询的条件的元组,返回TRUE,外层查询的某一行要被留下

有满足的元组,返回FALSE,外层查询的某一行要被删除

直到确定完所有student表中的元组时,

再对留下的元组进行外部查询。

查询未选修0101号课程的student学号、姓名、性别

use teaching
go
select sno,sname,gender
from student as s
where not exists(
select *
from sc
where s.sno = sc.sno and sc.cno = '0101')

1.9 集合查询

1. UNION(并)
select_statement
UNION [ALL]
select_statement
[UNION [ALL]
select_statement[...n]]

select_statement:select语句。

ALL不去重,不指定,就去重(默认)

选了‘0101’和‘0202’的课程的学生的名字。

use teaching
go
select sno,sname
from student
where sno in(
select sno
from sc as sc1
where exists(
select *
from sc as sc2
where sc1.sno = sc2.sno and sc1.cno = '0101' and sc2.cno = '0202'))
-- 或
use teaching
go
select sc.sno,student.sname
from student inner join sc on student.sno = sc.sno
where cno = '0101'
UNION
select sc.sno,student.sname
from student inner join sc on student.sno = sc.sno
where cno = '0202'
2. INTERSECT(交)和EXCEPT(差)

自动去重

select_statement
{INTERSECT|EXCEPT}
select_statement

1.10 排序(ORDER BY)

最终的查询结果进行排序默认ASC升序,DESC降序。

ORDER BY不能在子查询中使用。

ORDER BY 多个,先按第一个排,再按第二个排……

1.11 存储查询结果

SELECT select_list INTO new_table_name   ,自动创建一个新表

FROM……

……

2、数据操作中使用SELECT字句

2.1 INSERT中使用SELECT字句

INSERT INTO table_name[(column_list)]
SELECT select_list
FROM table_name
[WHERE condition_expression]

要自己创建表。

创建一个及格的成绩表

use teaching
go
create table 成绩表(
姓名 char(8),
学号 char(4),
成绩 int)
go
insert into 成绩表(姓名,学号,成绩)
select * from sc where score>=60
go
select * from 成绩表

2.2 UPDATE中使用SELECT字句

UPDATE table_name
SET column_name=expression[,...n]
[WHERE condition_expression]

在condition_expression中使用select子句。

2.1 DELETE中使用SELECT字句

DELETE table_name
[WHERE condition_expression]

在condition_expression中使用select子句。


http://www.mrgr.cn/news/82283.html

相关文章:

  • 解决 pip install 出现 error: subprocess-exited-with-error 错误的方法
  • html中鼠标位置信息
  • LabVIEW滤波器功能
  • Python语言的字符串处理
  • Android 15应用适配指南:所有应用的行为变更
  • Redis数据结构服务器
  • GRAPE——RLAIF微调VLA模型:通过偏好对齐提升机器人策略的泛化能力(含24年具身模型汇总)
  • 矩阵的因子分解1-奇异值分解
  • 本地LLM部署--llama.cpp
  • Go 语言:Jank 简客博客系统
  • 我在广州学 Mysql 系列——插入、更新与删除数据详解以及实例
  • 数据结构与算法Python版 拓扑排序与强连通分支
  • chatwoot 开源客服系统搭建
  • 我的 2024 年终总结
  • 【信号滤波 (中)】采样条件及多种滤波算法对比(滑动平均/陷波滤波)
  • 【机器学习】工业 4.0 下机器学习如何驱动智能制造升级
  • JSON 系列之3:导入JSON标准示例数据
  • Redis - 1 ( 11000 字 Redis 入门级教程 )
  • SpringBoot整合Canal+RabbitMQ监听数据变更
  • gitlab的搭建及使用
  • uniapp H5 对接 声网,截图
  • 【Java回顾】 Day1简介-----变量命名规则
  • 时间序列预测算法---LSTM
  • Git的使用流程(详细教程)
  • Anaconda+PyTorch(CPU版)安装
  • net core介绍