如果两个不同表中的两个相同列具有值,如何返回true?SQL
回到这里,再问你们一个问题 我们有两个数据透视表,表A和表B。两个表都有相同的列 我试图编写一个查询,该查询将遍历每一行,仅当具有相同ID的记录在“value”字段中有值时才返回true。 因此,例如,对于记录ID3,表_A具有“sdnbfsj”值,而对于相同的记录ID,表_B具有“value”字段下的值 查询应该能够验证两个表中的同一单元格是否有值,如果有,则返回true。如果表总共只有三条记录,则返回true,但请注意,记录ID 5在表_a中有一个值,而记录ID 5在表_B中为NULL。因此查询应返回false 这就是我到目前为止所做的:如果两个不同表中的两个相同列具有值,如何返回true?SQL,sql,pivot-table,Sql,Pivot Table,回到这里,再问你们一个问题 我们有两个数据透视表,表A和表B。两个表都有相同的列 我试图编写一个查询,该查询将遍历每一行,仅当具有相同ID的记录在“value”字段中有值时才返回true。 因此,例如,对于记录ID3,表_A具有“sdnbfsj”值,而对于相同的记录ID,表_B具有“value”字段下的值 查询应该能够验证两个表中的同一单元格是否有值,如果有,则返回true。如果表总共只有三条记录,则返回true,但请注意,记录ID 5在表_a中有一个值,而记录ID 5在表_B中为NULL。因此
SELECT source_column from
(
select source_column from Table_A WHERE Value_Inserted=1
EXCEPT
select source_column from Table_B WHERE Value_Inserted=1
)X
WHERE source_column IN
(
'Col_1'
,'COl_2'
,'Col_3'
,'Col_4'
)
您应该能够这样做,首先将
id
上的两个表连接起来,然后根据您的情况进行计数。然后。。。返回“真”或“假”
语法可能因您使用的RDBMS而异 我想你想要:
select min(case when a.value is not null and a.value is null then 0
when a.value is null a and a.value is not null then 0
else 1
end) as flag
from a join
b
on a.class = b.class and a.source_col = b.source_col;
这将1视为真,0视为假。取决于您使用的DBMS
对于Oracle使用nvl2
功能:
select nvl2(a.value, 1, 0) * nvl2(b.value, 1, 0)
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
select ( case when IfNull(a.value,'0') = '0' then '0'
else '1'
end )
*
( case when IfNull(b.value,'0') = '0' then '0'
else '1'
end ) as result
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
select cast( ( case when coalesce(a.value,'0') = '0' then '0'
else '1'
end ) as int )
*
cast( ( case when coalesce(b.value,'0') = '0' then '0'
else '1'
end ) as int ) as result
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
对于MSSQLconvert
或cast
功能可以使用:
select convert(int, ( case when IsNull(a.value,'0') = '0' then '0'
else '1'
end ))
*
convert(int, ( case when IsNull(b.value,'0') = '0' then '0'
else '1'
end )) as result
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
对于MySQL使用IfNull
函数:
select nvl2(a.value, 1, 0) * nvl2(b.value, 1, 0)
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
select ( case when IfNull(a.value,'0') = '0' then '0'
else '1'
end )
*
( case when IfNull(b.value,'0') = '0' then '0'
else '1'
end ) as result
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
select cast( ( case when coalesce(a.value,'0') = '0' then '0'
else '1'
end ) as int )
*
cast( ( case when coalesce(b.value,'0') = '0' then '0'
else '1'
end ) as int ) as result
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
对于PostGreSQL使用coalesce
函数:
select nvl2(a.value, 1, 0) * nvl2(b.value, 1, 0)
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
select ( case when IfNull(a.value,'0') = '0' then '0'
else '1'
end )
*
( case when IfNull(b.value,'0') = '0' then '0'
else '1'
end ) as result
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
select cast( ( case when coalesce(a.value,'0') = '0' then '0'
else '1'
end ) as int )
*
cast( ( case when coalesce(b.value,'0') = '0' then '0'
else '1'
end ) as int ) as result
from Table_A a
inner join Table_B b
on (a.ID = b.ID);
它们都给出了以下结果
result
0
0
1
0
0
你的数据库引擎是什么???请在这里发布你希望看到的输出。另外,请以纯文本而不是图像的形式输入数据