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

Sql server SQL Server存储过程选择,存在,多个表,sql-server,sql-server-2008,join,stored-procedures,Sql Server,Sql Server 2008,Join,Stored Procedures,有什么方法可以做到这一点吗 表1 表2 我想: 如果表2中存在表1中的所有记录,则从表1中选择,其中…条件 如果表1中不存在记录,请从表1中选择所有记录 合并两个选择结果。按创建日期对所有结果进行排序。 例如,结果应为: 结果 您可以通过执行以下操作来实现此目的: 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.Created

有什么方法可以做到这一点吗

表1

表2

我想:

如果表2中存在表1中的所有记录,则从表1中选择,其中…条件 如果表1中不存在记录,请从表1中选择所有记录 合并两个选择结果。按创建日期对所有结果进行排序。 例如,结果应为:

结果


您可以通过执行以下操作来实现此目的:

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;

看,我假设条件是t2.id=4,但根据表中的其他数据,它可以是任何其他数据。

希望这能有所帮助

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 

可能有多种解决方案。 单程 我们可以使用两个不同的查询获得结果集,最后使用UNION将两个结果集合并

另一方面,, 第一条语句是,如果表2中存在表1中的所有结果集,则从表1中获取该结果集,并在where子句中使用一些条件 意味着使用内部连接我们可以实现这一点 第二条语句是从表1中获取表2中不存在的所有结果集 如果表2中不存在表1的数据,则与内部联接查询一起还包括表1的数据 在这里,我们可以借助左侧的表1的左侧外部连接

假设:条件:t1.Id!=四,

让我们尝试使用上述两种方法来理解查询

---- -- --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;
-在这个查询中,我们在执行联接操作后使用这些条件。 -因此,在根据连接条件过滤出结果集后,将应用此条件 -如果表1中有任何空记录,那么join中使用的列Id将不会出现在最终结果集中

-为了避免这种情况,我们可以在标准中包含空检查

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

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;

谢谢大家的回复

我给出了我想要的答案:

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)

1-你忘记了条件。2-从表1中选择t2.ID t1将失败。3-为什么在。。。t1除了。。。t2而不是不在。。。t2?4-性能不是很好,看看执行计划。
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;
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;
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)