当前位置:网站首页>数据库学习 – select(多表联查)[通俗易懂]
数据库学习 – select(多表联查)[通俗易懂]
2022-07-21 15:52:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
多表联合查询
多表联合查询可以通过连接运算实现,而连接运算又可以通过广义笛卡尔积后在进行选择运算来实现。
Select 多表联合查询语句
Select 列名 [[,列名]…] from 表名1,表名2,,… Where 查询条件;
可以使用as为表或者字段(属性)取别名;
Select 列名 [[,列名]…] from 表名1 [as] 别名1,表名2 [as} 别名2,,… Where 查询条件;
Select 列名1 [as] 列别名1 [[,列名2 [as] 列别名2]…] from 表名1 [as] 表别名1,表名2 [as] 表别名2,,… Where 查询条件;
“[]”表示其中的内容可以省略;
查询条件中要包含连接条件,通过不同的连接条件可以实现等值连接、不等值连接等各种连接。
示例:
原表数据:
学生表 student
老师表 teacher:
课程表 course:
选课表 student_course:
1 按“0001”号课程成绩由高到低顺序显示所有学生学号、姓名、成绩(二表连接);
SELECT
student.student_id,
student.student_name,
student_course.score
FROM
student,
student_course
WHERE
student.student_id = student_course.student_id
AND student_course.course_id = '0001'
ORDER BY
student_course.score DESC;
当两个(多个)表中有相同的字段(属性,列名)时,使用表名(或表别名).列名的形式对查询内容和条件内容进行区分。
运行结果:
2 按“高等数学”课程成绩由高到低顺序显示所有学生姓名、学号、分数、课程名(三表连接);
SELECT
st.student_id,
st.student_name,
sc.score,
co.course_name
FROM
student AS st,
student_course AS sc,
course AS co
WHERE
st.student_id = sc.student_id
AND sc.course_id = co.course_id
AND co.course_name = '高等数学'
ORDER BY
sc.score DESC;
运行结果:
3 查询有薪水差额的任意两位教师(单表连接查询);
SELECT
t1.teacher_id AS '教工号1',
t1.teacher_name AS '教师姓名1',
t2.teacher_id AS '教工号2',
t2.teacher_name AS '教师姓名2'
FROM
teacher t1,
teacher t2
WHERE
t1.teacher_salary > t2.teacher_salary;
运行结果:
(数据较多,这里只显示一部分)
4 查询既学过“0001”又学过“0002”号课程的所有学生学号;
SELECT
sc1.student_id '学号'
FROM
student_course sc1,
student_course sc2
WHERE
sc1.student_id = sc2.student_id
AND sc1.course_id = '0001'
AND sc2.course_id = '0002';
运行结果:
5 查询“0001”号课程比“0002”号课程成绩低的所有学生学号姓名;
SELECT
st.student_id '学号',
st.student_name '姓名'
FROM
student_course sc1,
student_course sc2,
student st
WHERE
sc1.student_id = sc2.student_id
AND sc1.student_id = st.student_id
AND sc1.course_id = '0001'
AND sc2.course_id = '0002'
AND sc1.score < sc2.score;
运行结果:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124750.html原文链接:https://javaforall.cn
边栏推荐
- Oracle about date field index usage test
- 31下一个排列
- Azure key vault (3): the difference between key and secret
- Dynamics CRM: 邮箱配置(三) - 配置Email Server Profiles和Mailboxes
- Detailed explanation of redis [data operation, persistence, jedis, cache processing] of NoSQL database
- 287寻找重复数
- Dynamics 365: how to create users in azure AD and add them to dynamics 365 Online
- Keras深度学习实战(10)——迁移学习
- Azure key vault (2): create azure key vault
- 迪赛智慧数——其他图表(桑基图):暑假消费情况
猜你喜欢
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
Dynamics 365: 推荐几个XrmToolBox中创建ER(Entity Relationship)图的插件
使用C#控制台程序读取Azure Service Bus队列中的消息
Keras' deep learning practice -- gender classification based on inception V3
Tmech publishes the latest progress in the operation control technology of the must choose: to realize the high robustness walking of humanoid robots
Dynamics CRM: 批量导入数据来更新记录的注意事项
Detailed explanation of redis [data operation, persistence, jedis, cache processing] of NoSQL database
Dynamics CRM: among the locally deployed servers, sandbox, unzip, VSS, asynchronous, and monitor services are also available
電腦上如何開啟多個微信,微信多開
22括号生成
随机推荐
浅谈负载
Dynamics CRM: 查询字符串参数(Query String Parameters)的使用方法
Is there any risk in the bank's robust financial products? Will the principal be lost?
Pycharm reads jy-901s data
Application between azure service bus and dynamics 365 service endpoint
同花顺开户能直接开吗?开户安全吗?怎么办理开户??
How to use document tools for API management?
解析字符串
Keras deep learning practice (10) -- transfer learning
Dynamics CRM: among the locally deployed servers, sandbox, unzip, VSS, asynchronous, and monitor services are also available
Dynamics 365: 如何在Azure AD中创建用户并添加到Dynamics 365 Online中
Dynamics CRM:表单中移除一个控件时,提示“The field you are trying to remove is required by the system or business“
Yyds dry goods inventory # solve the real problem of famous enterprises: how many people will little a meet at most
感觉的定义
Keras deep learning practice (11) -- visual neural network middle layer output
Comment ouvrir plusieurs wechat sur l'ordinateur
Keras深度学习实战(15)——从零开始实现YOLO目标检测
Dynamics 365: 详解插件的执行深度(PluginExecutionContext.Depth)
无偿分享 简单易用 贝茨训练法 调解睫状肌 近视恢复训练
Dynamics 365: explain the execution depth of plug-ins (pluginexecutioncontext.depth)