如何在SQL中合并来自多个表的数据 [英] How to merge data from multiple tables in SQL

查看:118
本文介绍了如何在SQL中合并来自多个表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


猜猜我处在一个复杂的情况中.这是场景.
我在SQL Server中有3个表.
招生,学生,课程. (仅供参考,没有外键)
表格列是
学生-学生编号,学生姓名
课程-课程ID,课程名称
注册-注册ID,学生ID,课程ID,课程结果


Guess I am in a complex situation. Heres the scene.
I have 3 tables in SQL Server.
Enrollment, Student, Course. (FYI, there are no foreign keys)
The table columns are
Student - StudentId, StudentName
Course - CourseId, CourseName
Enrollment - EnrollmentId, StudentId, CourseId, CourseResult

样本数据
学生-s1,Sid
课程-C1,科学
报名-1,s1,c1,80

Sample Data
Student - s1, Sid
Course - c1, Science
Enrollment - 1, s1, c1, 80

我想要一个SQL查询,该查询选择如下所示的数据

I would want a SQL query that selects data like below

1,s1,Sid,c1,Science,80

1, s1, Sid, c1, Science, 80

我在DAL层通过多次调用数据库很难做到.但是想在一个电话中和在数据库中做到这一点.

I did it hard way in the DAL layer with multiple calls to database. But would like to do it in one call, and in DB.

任何人都可以营救!

推荐答案

使用联接.

select enrollment.*, student.*, course.* from enrollment
   inner join Student on enrollment.studentId = student.studentId
   inner join Course on enrollment.courseId = course.courseId

这篇关于如何在SQL中合并来自多个表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆