温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SQL语言的查询方法有哪些

发布时间:2021-12-22 15:12:29 来源:亿速云 阅读:263 作者:iii 栏目:编程语言

这篇文章主要讲解了“SQL语言的查询方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SQL语言的查询方法有哪些”吧!

外键约束

MySQL属于关系型的数据库,表之间可以建立关系,如:学生表和成绩表,在成绩表中添加学生编号引用学生表中的学生编号,这样在成绩表中就不用添加重复的学生信息了,这种关系也叫主外键关系,可以通过设置外键约束实现。

可以在创建表时,添加外键约束来保证表和表之间引用完整性,添加外键后:

在插入外键表数据前,必须先插入主表数据

在删除主表数据前,必须先删除外键表数据

语法:

create table 表名

(

字段名 类型 约束,

... ,

constraint 外键名称 foreign key (外键列) references 主表(主键)

);

代码示例:

use mysql_db;

-- 创建成绩表

drop table if exists tb_score;

create table tb_score

(

score_id int primary key auto_increment,

score_stu_id int,

score int,

score_course varchar(20),

constraint fk_score_stu_id foreign key(score_stu_id) references tb_student(stu_id)

);

内连接查询

在查询时我们经常要把相关的多张表的字段,一起查询出来,如查询学生成绩时,要显示分数和学生姓名。这个时候我们就需要连接查询了,连接查询分为内连接和外连接,我们先学习内连接查询。

内连接查询的特点是:会查询出相关表中都存在的数据。

语法有两种实现方法:

1)select 字段..... from 表1 inner join 表2

on 表1.主键 = 表2.外键;

注意:这里假设表1是主表,内连接表的前后顺序无关

2)select 字段..... from 表1 , 表2

where 表1.主键 = 表2.外键;

代码示例:

-- 查询学生姓名和成绩 方式1

select s.stu_id ,s.stu_name,c.score_course,c.score from tb_score c inner join tb_student s on s.stu_id = c.score_stu_id;

-- 方式2

select s.stu_id ,s.stu_name,c.score_course,c.score from tb_score c , tb_student s where s.stu_id = c.score_stu_id;

效果相同:

SQL语言的查询方法有哪些cdn.xitu.io/2019/6/5/16b265444d53bca3?w=300&h=196&f=png&s=35605">

外连接查询

外连接分为左外连接和右外连接:

1) 左外连接

连接查询多张表的数据,显示所有左表的数据,右表存在不相符的数据补null。

语法:

select 字段... from 左表 left join 右表

on 主表.主键 = 子表.外键;

代码示例:

-- 左外连接,查询学生姓名和成绩

select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id;

SQL语言的查询方法有哪些

-- 查询所有参加过考试的同学

select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id where c.score is not null;

SQL语言的查询方法有哪些

-- 查询所有没参加过考试的同学

select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s left join tb_score c on s.stu_id = c.score_stu_id where c.score is null;

SQL语言的查询方法有哪些

2)右外连接

与左连接相反,显示所有右表数据,左表中不相符的数据补null。

语法:

select 字段... from 左表 right join 右表

on 主表.主键 = 子表.外键;

代码示例:

select s.stu_id,s.stu_name,c.score_course,c.score from tb_score c right join tb_student s 
on s.stu_id = c.score_stu_id;

SQL语言的查询方法有哪些

子查询

在查询语句中还可以嵌入查询语句,嵌入的查询也叫子查询,子查询将先执行,查询到结果后,父查询可以将此结果作为查询条件再进行一次查询,这样可以解决比较复杂的查询问题。

语法:

select ... from 表 where 字段 比较运算符 (select ... from 表 where 条件);

代码示例:

-- 查询年龄比李四大的学生

select * from tb_student where stu_age > (select stu_age from tb_student where stu_name = '李四');

SQL语言的查询方法有哪些

-- 查询李四的老乡

select * from tb_student where stu_address = (select stu_address from tb_student where stu_name = '李四');

SQL语言的查询方法有哪些

子查询之IN

有时候当子查询中查询结果不止一个的情况下,使用比较运算符会出现错误,这时候我们就需要使用一些关键字来帮助筛选结果。

in关键字的作用是在字段和数据列表中任意一个相等,条件就成立。

代码示例:

-- 查询语文分数考相同的学生,先用子查询查语文的成绩,在用内连接查考过语文的学生姓名和成绩,把成绩进行比较

select stu_name,score_course,score from tb_student inner join tb_score on tb_student.stu_id = tb_score.score_stu_id where score_course='语文' and score in(select score from tb_score where score_course = '语文');

SQL语言的查询方法有哪些

子查询之ALL

all和比较运算符配合使用,如果字段和所有的查询结果都比较成立,结果才成立。

语法:

字段 比较运算 all(查询结果)

代码示例:

-- 查询比所有男学生小的女学生,先查所有男学生的年龄,如果女学生年龄比所有这些年龄大,就查出来

select stu_name,stu_age,stu_gender from tb_student where stu_gender = '女' and stu_age < all(select stu_age from tb_student where stu_gender = '男');

SQL语言的查询方法有哪些

子查询之ANY

any和比较运算符配合使用,如果字段和任意一个查询结果比较成立,则结果成立。

语法:

字段 比较运算 any(查询结果)

代码示例:

-- 查询只要比一个南京学生大的武汉学生

select stu_name,stu_address from tb_student where stu_address = '武汉'

and stu_age > any(select stu_age from tb_student where stu_address='南京');

SQL语言的查询方法有哪些

子查询之Exists

exists表示是否有查询结果,如果没有结果,返回false,有结果则返回true

语法:

exists(查询结果)

-- 查询考过英语的同学,在子查询中需要判断父查询中的学生id是否在子查询中存在

select * from tb_student where

exists(select score_stu_id from tb_score where tb_student.stu_id = tb_score.score_stu_id and score_course = '英语');

感谢各位的阅读,以上就是“SQL语言的查询方法有哪些”的内容了,经过本文的学习后,相信大家对SQL语言的查询方法有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sql
AI