Sql 如何以线性方式将in子句中的值列表与另一个in子句中的值进行匹配
我希望输出匹配只包含“col1”中的“123”和“col2”中的“abc”的行,而不是包含“col1”中的“123”和“col2”中的“def”的行。 子句中的列表应以线性方式相应地匹配Sql 如何以线性方式将in子句中的值列表与另一个in子句中的值进行匹配,sql,oracle,Sql,Oracle,我希望输出匹配只包含“col1”中的“123”和“col2”中的“abc”的行,而不是包含“col1”中的“123”和“col2”中的“def”的行。 子句中的列表应以线性方式相应地匹配 --Table_1 col1 col2 ............ 123 abc 456 def 123 def select * from Table_1 where col1 in (123,456) and col2 in (abc,def); O/p 您可以尝试使用
--Table_1
col1 col2
............
123 abc
456 def
123 def
select * from Table_1 where col1 in (123,456) and col2 in (abc,def);
O/p
您可以尝试使用
row\u number
window函数进行设置
col1 col2
123 abc
456 def
您可以使用元组来比较多个列的组合
SELECT col1,col2
from (
select col1,col2,row_number() over(partition by col1 order by col2) rn
from Table_1
where col1 in (123,456) and col2 in ('abc','def')
) t1
where rn = 1
为什么不在where子句中声明条件,比如:where(col1=123和col2='abc')或(col1=456和col2='def')?@assembly.jc我有一个巨大的列表要放在col1和col2中,上面的方法会很繁琐。你的dbms是什么?@D-Shih Oracle sqldeveloper@D-施,我在试着理解你的答案,你为什么要删除它,它起作用了,你能写回去吗?但是所有“col2”的值都应该用这种方法手动输入。我不明白,为什么表之间要用完全不同的“count”连接起来,你能详细说明一下吗?因为
cte2
会得到col2
total的值,cte1
将通过每个col1
获得金额,然后如果cte2
中的金额等于cte1
意味着cte1
的col1
具有完整金额@senorelanzaOkay,但将有多个不同的值,与col2类似,col2可以有两个或三个不同的值,并且不可能基于cte2的“cnt”联接表,而且在联接之后,如何能够以线性方式获取记录。是否应选择col1中的记录456?@senorelanza。这是正确的答案。Oracle支持在中使用的元组。我建议接受这个答案。
SELECT col1,col2
from (
select col1,col2,row_number() over(partition by col1 order by col2) rn
from Table_1
where col1 in (123,456) and col2 in ('abc','def')
) t1
where rn = 1
select *
from Table_1
where (col1,col2) in ( (123,'abc'),(456,'def'), (789,'abc') );