如何将3个表连接到sqlserver 2005中的一个表中 [英] How to join 3 tables into one table in sqlserver 2005

查看:141
本文介绍了如何将3个表连接到sqlserver 2005中的一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家!!



更新到Sqlserver 2005数据库。



如何加入3 table ...到sqlserver中的一个表



所以,这是我的要求:



我有我的数据库中的3个表...作为StudentCodes,名称,标记



1)从第一个表[StudentCodes表],我想带学生ID

2)从第二张表[姓名表],我想取学生姓名,StudentFathersName

3)从第3表[商标表],我想采取总商标,百分比


最后我想把这3张桌子放到一张桌子里。



请你能建议我,怎么样将这3个表连接到一个表。



非常感谢提前

解决方案

你需要阅读关于SQL JOINS。



这里:

W3Schools:SQL加入 [ ^ ]

SQL教程 - SQL连接 [ ^ ]

CP:SQL加入 [ ^ ]

MSDN:加入基础知识 [ ^ ]


亲爱的Ranjith,



1st你应该在所有3个表中都有共同的id,假设有学生。

 选择 a.studentid,b.name,b.fathername,c.marks,c。百分比 来自 
Stu dentCodes a 使用 Nolock ),名称b 使用 Nolock ),标记c 使用 Nolock
其中 a.studentid = b.studentid b.studentid = c .studentid a.studentid = c.studentid





这是基本的例如。希望它会对你有所帮助。



问候,

AP


很可能你没有意味着您要从这些表中加入(创建)单个表。是否要基于它们创建结果集。如果是这种情况,正如Sandeep指出的那样,你需要加入。



因此,要选择单个结果集,您的查询可能如下所示:

  SELECT  sc.StudentId,
n.StudentName,
n.StudentFathersName,
m.TotalMarks,
m.Percentage
FROM StudentCode sc
INNER JOIN 名称n ON n.StudentId = sc.StudentId
INNER JOIN 标记m ON m.StudentId = sc.StudentId


Dear Experts !!

Am fresher to Sqlserver 2005 Database.

How to join 3 tables... to one table in sqlserver

so, here is my requirement :

I have 3 tables in my Database...as StudentCodes,Names,Marks

1)From First table[StudentCodes Table], i want to take StudentId
2)From second Table[Names Table], i want to take Student Name , StudentFathersName
3)From 3rd Table[Marks Table], I want to take Total Marks, Percentage

and Finally i want to make these 3 tables into one table.

Please can u suggest me, how to join these 3 tables to one Table.

Thanks a lot in Advance

解决方案

You need to read about SQL JOINS.

Here:
W3Schools: SQL Joins[^]
SQL Tutorial - SQL Joins[^]
CP: SQL Joins[^]
MSDN: Joins Fundamentals[^]


Dear Ranjith,

1st u should have common id in all 3 tables, suppose Studentid.

Select a.studentid,b.name,b.fathername,c.marks,c.percent from 
StudentCodes a With (Nolock), Names b With (Nolock), Marks c With (Nolock)
where a.studentid=b.studentid and b.studentid=c.studentid and a.studentid=c.studentid



this is basic eg. hope it will help you.

Regards,
AP


Most likely you don't mean that you want to join (create) a single table from those tables. Do you want to create a result set based on them. If that is the case, as Sandeep pointed out, you need a join.

So to select a single result set, your query could look something like:

SELECT sc.StudentId,
       n.StudentName,
       n.StudentFathersName,
       m.TotalMarks,
       m.Percentage
FROM   StudentCode sc 
       INNER JOIN Names n ON n.StudentId = sc.StudentId
       INNER JOIN Marks m ON m.StudentId = sc.StudentId


这篇关于如何将3个表连接到sqlserver 2005中的一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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