sql server如何获取联接查询 [英] sql server how to get join query
问题描述
大家好
我在那有两个桌子
1)TechnologyMaster
TechnologyId(PK)int
TechnologyName varchar
2)标题大师
TitleId(PK)int
ProjectTitle varchar
TechnologyId(FK)int
DomainId(FK)int
我希望所有projecttitle和technologyname如何编写联接查询.请帮助我
Hi all
I have two tables in that
1) TechnologyMaster
TechnologyId(PK) int
TechnologyName varchar
2)Title Master
TitleId(PK) int
ProjectTitle varchar
TechnologyId(FK) int
DomainId(FK) int
I want all projecttitle and technologyname how to write join query. please help me
推荐答案
SELECT tim.ProjectTitle, tem.TechnologyName
FROM dbo.TitleMaster tim
INNER JOIN
dbo.TechnologyMaster tem
ON tim.TechnologyId = tem.TechnologyId
--WHERE: Add an optional where clause here if you need one
另请参阅: http://www.w3schools.com/sql/sql_join_inner.asp [ ^ ]或此 ^ ].
问候,
Please also refer to this: http://www.w3schools.com/sql/sql_join_inner.asp[^] or this Using Inner Joins[^].
Regards,
SELECT Tech.TechnologyName, Tm.ProjectTitle
FROM TechnologyMaster Tech
INNER JOIN
Titlemaster Tm
ON Tech.TechnologyId = Tm.TechnologyId
这似乎是一个相当简单的联接,所以我假设您不熟悉联接.要联接两个表,您需要根据一个公共值将它们连接起来.在这种情况下,TechnologyId字段包含相等的值(数字表示TechnologyMaster表中的一行).现在我们需要做一个假设:我们是否要让一个表中的所有记录与另一表中的值匹配,还是只希望两边都匹配的行.例如,如果我们在TechnologyMaster表中有一条记录在Title Master表中没有对应的一行,那么我们是否要显示TechnologyMaster中的该行?反之亦然(尽管不太可能,因为TitleMaster表似乎依赖于TechnologyMaster表).我假设我们想要两边都匹配的行.这称为INNER JOIN,这是最常见的联接类型.这是我们的处理方式:
This seems like a fairly simple join, so I''m going to assume you aren''t familiar with joins. To join two tables, you need to connect them based upon a common value. In this case, the TechnologyId field contains the values that are equal (the number represents a row in the TechnologyMaster table). Now we need to make an assumption: do we want all the records from one table and the values from the other table that match or do we want only the rows that match on both sides. For instance, if we have a record in the TechnologyMaster table that doesn''t have a corresponding row (or rows) in the Title Master table, do we want that row from TechnologyMaster to show? The same is true in reverse (although it is less likely since the TitleMaster table seems to be dependent on the TechnologyMaster table). I will assume we want the rows where there is a match on both sides. This is called an INNER JOIN and it is the most common type of join. Here is how we would do this:
SELECT te.TechnologyName, ti.ProjectTitle
FROM TechnoloogyMaster te
INNER JOIN TitleMaster ti ON te.TechnologyId = ti.TechnologyId
请注意,我重命名了表格以使其更短(te代替TechnologyMaster,ti代替TitleMaster).这只是为了简化事情.
Note that I renamed the tables to make them shorter (te instead of TechnologyMaster and ti instead of TitleMaster). This is just to simplify things.
这篇关于sql server如何获取联接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!