我有一个要求,当ID都相同时,我需要显示两个表的数据。当第一个表中存在id而第二个表中没有id时,我需要显示第一个表中的数据
CREATE TABLE [dbo].[TEST](
[ID] [int] NULL,
[Name] [varchar](10) NULL,
[Status] [char](1) NULL,
[CreatedDate] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Test_History](
[ID] [int] NULL,
[Name] [varchar](10) NULL,
[Status] [char](1) NULL,
[CreatedDate] [datetime] NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Test_History] Script Date: 06/19/2015 19:01:49 ******/
INSERT [dbo].[Test_History] ([ID], [Name], [Status], [CreatedDate]) VALUES (1, N'Mohan', N'A', CAST(0x0000A4BC01347E88 AS DateTime))
INSERT [dbo].[Test_History] ([ID], [Name], [Status], [CreatedDate]) VALUES (1, N'Mohan', N'I', CAST(0x0000A4BC0134A390 AS DateTime))
INSERT [dbo].[Test_History] ([ID], [Name], [Status], [CreatedDate]) VALUES (2, N'Rohan', N'A', CAST(0x0000A4BC01391FCC AS DateTime))
/****** Object: Table [dbo].[TEST] Script Date: 06/19/2015 19:01:49 ******/
INSERT [dbo].[TEST] ([ID], [Name], [Status], [CreatedDate]) VALUES (2, N'Rohan', N'I', CAST(0x0000A4BC0138D584 AS DateTime))
INSERT [dbo].[TEST] ([ID], [Name], [Status], [CreatedDate]) VALUES (1, N'Mohan', N'A', CAST(0x0000A4BC013072DC AS DateTime))
INSERT [dbo].[TEST] ([ID], [Name], [Status], [CreatedDate]) VALUES (3, N'Raj', N'A', CAST(0x0000A4BC0138DED7 AS DateTime))
INSERT [dbo].[TEST] ([ID], [Name], [Status], [CreatedDate]) VALUES (4, N'Krishna', N'A', CAST(0x0000A4BC0138EE31 AS DateTime))
到目前为止,我已经尝试了我的查询来实现结果
select T.ID,COALESCE(T.ID,TT.ID),T.Name,COALESCE(T.Name,TT.Name),T.status,COALESCE(T.status,TT.status)
from Test T LEFT JOIN (Select TOP 1 ID,MIN(Name)name,Status from Test_History
GROUP BY ID,status
)TT
ON T.ID = TT.ID
where T.ID = 3
Id = 1 and 2 present show i will get data from both tables
Id = 3 and 4 not present in the table
so using coalesce i will get the data
从第一个表开始,并在第二个表列中显示
但是还有其他任何方式,比如两个表都是相同的结构
我在考虑
Declare @tablename varchar(10)
IF EXISTS (SELECT 1 from TESt where id = @id)
IF COunt there in both tables
SET @tablename = Test
ELSE
SET @tablename = Test_history
select * from @tablename where id = @ID
我可以得到像这样的任何解决方案
答案 0 :(得分:1)
首先:感谢您对问题相关数据的出色设置!
如果你真正的问题是如果你的问题中描述的表变量可以使用,答案是否定的;或者更确切地说,它不值得。
不推荐:
declare @TableName TABLE (
[ID] [int] NULL,
[Name] [varchar](10) NULL,
[Status] [char](1) NULL,
[CreatedDate] [datetime] NULL)
IF EXISTS (SELECT 1 from TESt where id = @id)
INSERT INTO @TableName SELECT * FROM dbo.TEST WHERE ID = @ID
ELSE INSERT INTO @TableName SELECT * FROM dbo.[Test_History] WHERE ID = @ID
select * from @tablename where id = @ID
以下是我更喜欢的解决方案:
DECLARE @ID INT = 3;
SELECT * FROM [dbo].[TEST] ss WHERE ss.id = @id
UNION ALL SELECT * FROM [dbo].[Test_History] th WHERE th.id = @id
and not exists ( SELECT * FROM [dbo].[TEST] ss WHERE ss.id = @id);
UNION ALL
效果非常好 - 不要忘记ALL
关键字,我假设ID是PK或AK。
答案 1 :(得分:0)
您可以使用EXCEPT
。
以下是一个例子:
SELECT a,b
FROM (
VALUES (1, 2), (3, 4), (5, 6), (7, 8), (9, 10)
) AS MyTable(a, b)
EXCEPT
SELECT a,b
FROM (
VALUES (1, 2), (7, 8), (9, 10)
) AS MyTable(a, b);
这将返回upper语句的所有行,这些行不在第二个语句中。
答案 2 :(得分:0)
如果我理解正确并且您希望显示两个表之间匹配的所有记录,并且只显示同一结果集中第二个表中不存在id的第一个表中的记录,那么您只需要一个简单的左连接:
SELECT *
FROM dbo.test t
LEFT OUTER JOIN Test_History th
ON t.id = th.id
WHERE t.id = @id