2014-09-26 70 views
0

任何方法来做到这一点?SQL Server存储过程选择,存在,多个表

表1

1 
2 
3 
4 
5 

表2

3 (with the condition) 
4 (without the condition) 

我想:

  1. 选择从表1中的所有记录,如果它存在于表2,WH (条件)
  2. 如果表2中不存在,选择表1中的所有记录
  3. 合并两个选择结果。将所有结果与其创建日期进行排序。

举例来说,结果应该是:

结果

1 
2 
3 
5 

回答

0

感谢所有响应。

我拿出我想要的答案:

SELECT * 
FROM Table1 t1 
    WHERE NOT EXISTS(SELECT 1 FROM Table2 t2 
           WHERE t1.ID = t2.ID 
           AND t2.CIF_KEY = @CifKey 
           AND t2.STATUS <> ''3'') 
    AND (condition in where clause) 
0

您可以通过执行实现这一目标:

SELECT t1.id 
FROM Table1 t1 
LEFT JOIN Table2 t2 on t1.id = t2.id 
WHERE condition OR t2.id IS NULL 
ORDER BY t1.CreatedDate; 

fiddle(我假定条件是t2.id!=4,但它可以是任何其他取决于其他呃数据在你的表中)。

0

希望这可以帮助。

SELECT t1.* from table1 t1 
JOIN table2 t2 
ON t1.ID = t2.ID 

UNION ALL 

SELECT t1.* from table1 t1 where ID in 
(
    SELECT t2.ID from table1 t1 except Select t2.ID from table2 t2 
) 

ORDER BY t1.CreatedDate 
+0

1 - 你忘了条件。 2 - 'table1 t1'中的SELECT t2.ID将失败。 3 - 为什么'IN(... t1 EXCEPT ... t2)'而不是'NOT IN(... t2)'? 4 - 表现并不好([小提琴](http://sqlfiddle.com/#!3/03a84/7),看执行计划)。 – 2014-09-26 10:46:54

0

可能有多种解决方案。 一种方式 我们可以使用两种不同的查询得到的结果集,最后合并结果集使用UNION

的另一种方式, 首先声明是说,让所有的结果从TABLE1设置,如果它存在的两个TABLE2以及一些标准(在where子句中的条件) 表示使用INNER JOIN我们可以实现这个 第二个声明是说从TABLE1中获取所有不存在于TABLE2中的结果集 意味着与INNER JOIN ed一起查询还包括TABLE1的数据如果不存在TABLE2 这里我们可以借助LEFT OUTER JOIN(取左表TABLE1)

0 (!条件:t1.Id = 4)

假设

让我们尝试同时使用的上述方式

---- -- --Step1 Create table and insert records 
---- create table1 with Id int identity columsn 
--CREATE TABLE Table1 (Id INT IDENTITY(1,1), CreatedDate smalldatetime default(getdate())); 
--go 
---- insert 1st 5 integers into Table1 
--INSERT INTO Table1 DEFAULT VALUES 
--go 5 

---- create Table2 with Id int column 
--CREATE TABLE Table2 (Id INT , CreatedDate smalldatetime default(getdate())); 
--go 
---- insert records 3,5 into Table2 
--INSERT INTO Table2(Id) VALUES (3), (4); 

-- -- -- Solution: one way 
; WITH cteMyFirstResult AS 
(
    -- 2.1. Select all records from Table1 if it exists in Table 2, where...(condition) 
    SELECT 
     Id,  CreatedDate 
    FROM Table1 AS t1 
    WHERE t1.Id IN (SELECT Id FROM Table2 AS t2) 
    AND t1.Id != 4 -- assumption it can be any condition 
),cteMySecondResult AS (
    -- 2.2. Select all records from Table1 if it not exists in Table2 
    SELECT 
     Id,  CreatedDate 
    FROM Table1 AS t1 WHERE t1.Id NOT IN (SELECT Id FROM Table2 AS t2) 
) 
    -- 2.3. Combine both select results. Sort all results with their created date. 
    SELECT 
     Id, CreatedDate 
    FROM cteMyFirstResult 
UNION 
    SELECT 
     Id, CreatedDate 
    FROM cteMySecondResult 
    ORDER BY CreatedDate; 

理解查询 - - 解决方案:另一种方式(有错误)

SELECT t1.Id, t1.CreatedDate 
FROM Table1 AS t1 
LEFT JOIN Table2 AS t2 on t1.id = t2.id 
WHERE t1.Id != 4 
Order by T1.CreatedDate; 

- 在此查询中,我们在完成联接操作后使用条件。 - 因此,在基于JOIN条件过滤出结果集之后,将应用此条件 - 并且如果列Id中存在任何空记录(用于连接)不会出现在最终结果集

- 为了避免这一点,我们可以沿着包括NULL检查与我们的标准

- - 解决方案:另一种方式

SELECT t1.Id, t1.CreatedDate 
FROM Table1 AS t1 
LEFT JOIN Table2 AS t2 on t1.id = t2.id 
WHERE (t1.Id != 4) OR t1.Id IS NULL -- include all your criteria within small-barcket) 
Order by T1.CreatedDate;