INNER JOIN来自SQL SERVER的两个视图 [英] INNER JOIN two views from SQL SERVER

查看:95
本文介绍了INNER JOIN来自SQL SERVER的两个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试将两个视图组合成1.事实是两个视图都具有相同的列名。让我们说View 1有一个名为Test的列,View 2有一个名为Test的列。我需要制作一个新视图仅包含1个测试列,但包含前两个视图中的值。

解决方案

命名视图:

  SELECT  a.Col1,b.Col1,a.Col2,b.Col2,a.Col3  FROM  View1 a 
JOIN View2 b
ON a.Col1 = b。 Col1


我没有看到任何问题。

如下所示应该有效。

您的第一个观点

<前一个=SQL> 创建 查看 [TestView1]
AS
SELECT 1 AS Id,' 测试1' AS
UNION
SELECT 2 ' 测试2'

GO



输出1

 Id值
______________
1测试1
2测试2





您的第二个观点

 创建  VIEW  [TestView2] 
AS

< span class =code-keyword> SELECT 1 AS Id, 测试3' AS
UNION
SELECT 2 ' 测试4'

GO



输出2

 Id值
______________
1测试3
2测试4





您的第三个观点

  CREATE   VIEW  [TestView3] 
AS

SELECT FROM TestView1
UNION - 'UNION ALL'如果你想要包含重复记录
SELECT FROM TestView2

GO



输出3

价值
______
测试1
测试2
测试3
测试4





没有必要申请INNER JOIN,这是出于其他目的。



希望,它有帮助:)

如果这不能解决您的问题,请告诉我。


Hi i am trying to combine two views into 1.The thing is that both views have the same column names.Lets say View 1 has a column named Test and View 2 has a column named Test too.I need to make an new view to contain only 1 Test column but with values from both previous views.

解决方案

Name the views:

SELECT a.Col1, b.Col1, a.Col2, b.Col2, a.Col3 FROM View1 a
JOIN View2 b
ON a.Col1 = b.Col1


I don't see any problem doing that.
Something like following should work.
Your first view

CREATE VIEW [TestView1]
AS
SELECT 1 AS Id, 'Test 1' AS Value
UNION
SELECT 2,'Test 2'

GO


OUTPUT 1

Id	Value
______________
1	Test 1
2	Test 2



Your second view

CREATE VIEW [TestView2]
AS

SELECT 1 AS Id, 'Test 3' AS Value
UNION
SELECT 2,'Test 4'

GO


OUTPUT 2

Id	Value
______________
1	Test 3
2	Test 4



Your third view

CREATE VIEW [TestView3]
AS

SELECT Value FROM TestView1 
UNION --'UNION ALL' if you want to include duplicate records also
SELECT Value FROM TestView2

GO


OUTPUT 3

Value
______
Test 1
Test 2
Test 3
Test 4



There is no need to apply INNER JOIN which is there for some other purposes.

Hope, it helps :)
In case this doesn't resolve your problem, please let me know.


这篇关于INNER JOIN来自SQL SERVER的两个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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