Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 即使行不匹配,如何检查两个表具有相同的行值? 我有两个表考虑TBL1和TBL2 tbl1 Site --------- 101 - Hold 102 - test tbl2 Site --------- 101 - Hold 104 - wel 102 - test_Sql_Sql Server 2008 - Fatal编程技术网

Sql 即使行不匹配,如何检查两个表具有相同的行值? 我有两个表考虑TBL1和TBL2 tbl1 Site --------- 101 - Hold 102 - test tbl2 Site --------- 101 - Hold 104 - wel 102 - test

Sql 即使行不匹配,如何检查两个表具有相同的行值? 我有两个表考虑TBL1和TBL2 tbl1 Site --------- 101 - Hold 102 - test tbl2 Site --------- 101 - Hold 104 - wel 102 - test,sql,sql-server-2008,Sql,Sql Server 2008,我想检查tbl1中是否存在tbl2数据。如果是,select语句必须返回“是”,否则返回“否” EXample If tbl2 is not having "102-test" then also i have to return 'No' 我怎样才能做到这一点 您可以通过查找不匹配项并使用以下信息来执行此操作: select (case when count(*) = 0 then 'Yes' else 'No' end) from tbl2 t2 where not exists (sel

我想检查tbl1中是否存在tbl2数据。如果是,select语句必须返回“是”,否则返回“否”

EXample
If tbl2 is not having "102-test" then also i have to return 'No'

我怎样才能做到这一点

您可以通过查找不匹配项并使用以下信息来执行此操作:

select (case when count(*) = 0 then 'Yes' else 'No' end)
from tbl2 t2
where not exists (select 1 from tbl1 where t1.site = t2.site);
计数*保证只返回一行,包含是或否。

检查tbl1中是否存在tbl2数据,并在tbl1中是否存在tbl2时返回值为是或否的站点

不需要使用任何聚合,并且您拥有关于所有tbl1站点的信息-根据您的问题,这是您想要实现的

SELECT 
    a.site, CASE WHEN b.site IS NOT NULL THEN 'Yes' ELSE 'No END AS value_exists
FROM 
    tbl1 a
    LEFT JOIN tbl2 b ON
        a.site = b.site

如果您在TBL1中有重复的值,那么您可能需要考虑添加Sype或GROUPBY子句。

您可以检查我的更新问题吗?您想要一个返回值-是或否?对于整个表?还是每行一个返回值?
SELECT 
    a.site, CASE WHEN b.site IS NOT NULL THEN 'Yes' ELSE 'No END AS value_exists
FROM 
    tbl1 a
    LEFT JOIN tbl2 b ON
        a.site = b.site