如何在SQL Server 2005中检索数据 [英] How to retrieve data in sql server 2005

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

问题描述

我有一个表结构为
id | empname | type
1 rah hr
1猫先生

另一个表为
类型说明
hr他是hr
他是先生

我想编写一个sql查询,以便将数据显示为

id | empname |类型1 | desc1 | empname2 | type2 | desc2
1 raj hr他是hr cat先生他是mr

我该怎么办.
我已分层为

I have a table structure as
id|empname|type
1 raj hr
1 cat mr

and a another table as
type | description
hr he is hr
mr he is mr

I want to write a sql queries such that to should display data as

id | empname | type1 | desc1 | empname2 | type2 | desc2
1 raj hr he is hr cat mr he is mr

how can I do so.
i had tierd as

SELECT  dbo.empmst1.id, dbo.empmst1.name, dbo.emp1.description, dbo.emp1.types,(select
dbo.emp1.types as types2,dbo.emp1.description as desc2 from emp1)
FROM   dbo.empmst1 INNER JOIN
dbo.emp1 ON dbo.empmst1.types = dbo.emp1.types



但仍然没有结果,只有一个错误



but still no result, only an error

推荐答案

为您提供提示;通过将2个表与类型"列连接来开始查询.

请查看此处的示例;
Join_(SQL) [
Tip for you; start your query by joining 2 tables with the column "type".

Check here for an example;
Join_(SQL)[^]


尝试一下:
Try this:
SELECT dbo.empmst1.id, dbo.empmst1.name, dbo.emp1.description, dbo.emp1.types,
    dbo.emp1.types as types2, dbo.emp1.description as desc2
FROM dbo.empmst1
INNER JOIN dbo.emp1
ON dbo.empmst1.types = dbo.emp1.types



请注意,您不需要内部选择,因为表已经连接.



Notice you do not need the inner select as the table is already joined.


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

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