SQL Server存储过程选择,存在,多个表

时间:2014-09-26 09:32:03

标签: sql-server sql-server-2008 join stored-procedures

任何方法都可以吗?

表1

 1
 2
 3
 4
 5

表2

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

我想:

  1. 如果表1中存在,则选择表1中的所有记录,其中......(条件)
  2. 如果表2中不存在,则选择Table1中的所有记录
  3. 合并两个选择结果。使用创建日期对所有结果进行排序。
  4. 例如,结果应为:

    结果

     1
     2
     3
     5
    

4 个答案:

答案 0 :(得分: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,但根据您表格中的其他数据,它可以是其他任何内容。)

答案 1 :(得分: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 

答案 2 :(得分:0)

可能存在多种解决方案。 单程     我们可以使用两个不同的查询来获取结果集,最后使用UNION

组合两个结果集

另一种方式,    第一个语句是说,如果TABLE2中存在,则从TABLE1获取所有结果集以及某些条件(where子句中的条件)          意味着使用INNER JOIN我们可以实现这一点    第二个语句是从TABLE1获取所有结果集,这些结果集在TABLE2中不存在          与INNER JOIN一起使用的表示查询还包括TABLE1的数据(如果TABLE2中不存在)          在这里我们可以借助LEFT OUTER JOIN(左侧的TABLE1)

假设:(条件: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;

- - 解决方案:另一种方式(有bug)

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条件过滤掉结果集后,将应用此条件 - 如果Table1中的任何空记录用于列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;

答案 3 :(得分: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)