如何在MS Sql Server中获取不同的值 [英] How to get distinct value in MS Sql Server
本文介绍了如何在MS Sql Server中获取不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
描述表
ID Text Control_ID
1 hi 17
1 hhh 17
时间表
Id Time Control_ID
1 67 17
1 12 17
controlTable
control_id
17
19
需要显示:
hi 67 17
hhh 12 17
我上面有3个表,我想要合并记录。
Description Table
ID Text Control_ID
1 hi 17
1 hhh 17
Time table
Id Time Control_ID
1 67 17
1 12 17
controlTable
control_id
17
18
19
need to display:
hi 67 17
hhh 12 17
I have 3 tables as above and I want combined records.
推荐答案
试试这个:
Try this:
SELECT d.[Text], t.[time], d.control_id
FROM DescriptionTable AS d INNER JOIN TimeTable AS t ON d.control_id = t.control_id
>
DECLARE @DescriptionTable TABLE (ID INT, aText VARCHAR(30), Control_ID INT)
INSERT INTO @DescriptionTable (ID, aText, Control_ID)
SELECT ID, aText, Control_ID
FROM (
SELECT 1 As ID, 'hi' AS aText, 17 AS Control_ID
UNION ALL
SELECT 2 As ID, 'hhh' AS aText, 17 AS Control_ID ) AS T
DECLARE @Timetable TABLE (Id INT, aTime INT, Control_ID INT)
INSERT INTO @Timetable (Id, aTime, Control_ID )
SELECT 1 AS Id, 67 AS [Time], 17 AS Control_ID UNION ALL
SELECT 2, 12, 17
SELECT d.aText, t.aTime, d.control_id
FROM @DescriptionTable AS d INNER JOIN @TimeTable AS t ON d.ID = t.ID
有关加入数据的更多信息,请参阅: SQL连接的可视化表示 [ ^ ]
试用以下链接获取提示 -
HTTP://www.daniweb。 com / web-development / databases / mysql / threads / 413555 / how-do-i-use-distinct-inside-inner-join [ ^ ]
http://www.dbforums.com/microsoft-access/1640449-inner-join-distinct-clause.html [ ^ ]
Try out the following links for hints -
http://www.daniweb.com/web-development/databases/mysql/threads/413555/how-do-i-use-distinct-inside-inner-join[^]
http://www.dbforums.com/microsoft-access/1640449-inner-join-distinct-clause.html[^]
这篇关于如何在MS Sql Server中获取不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文