Sql 将两个select查询合并为一个查询
我有两个选择查询Sql 将两个select查询合并为一个查询,sql,sql-server,stored-procedures,sql-server-2016,Sql,Sql Server,Stored Procedures,Sql Server 2016,我有两个选择查询 select Amount, CurrentBalanceCurrency, AmountType from UserBalance where AmountType= 10 select Amount, CurrentBalanceCurrency, AmountType from UserBalance where AmountType= 20 我想在一行中进行此查询,而不必两次访问我的数据库。在where子句中使
select Amount,
CurrentBalanceCurrency,
AmountType
from UserBalance
where AmountType= 10
select Amount,
CurrentBalanceCurrency,
AmountType
from UserBalance
where AmountType= 20
我想在一行中进行此查询,而不必两次访问我的数据库。在
where
子句中使用中的运算符:
select Amount, CurrentBalanceCurrency, AmountType
from UserBalance
where AmountType in (10,20);
在where
子句中使用中的运算符:
select Amount, CurrentBalanceCurrency, AmountType
from UserBalance
where AmountType in (10,20);
您可以在
运算符中使用
查询
select [Amount], [CurrentBalanceCurrency], [AmountType]
from [UserBalance]
where [AmountType] in (10,20);
您可以在
运算符中使用
查询
select [Amount], [CurrentBalanceCurrency], [AmountType]
from [UserBalance]
where [AmountType] in (10,20);
您应在这两种情况之间使用或:
select Amount
,CurrentBalanceCurrency
,AmountType
from UserBalance where AmountType= 10 OR AmountType = 20
您应在这两种情况之间使用或:
select Amount
,CurrentBalanceCurrency
,AmountType
from UserBalance where AmountType= 10 OR AmountType = 20
另一种选择是使用union all连接结果集,这可以在某些情况下(您必须自己测试)比或或中的性能有所提高:
另一种选择是使用union all连接结果集,这可以在某些情况下(您必须自己测试)比或或中的性能有所提高:
你试过搜索吗?我建议查找语法。AmountType IN(10,20)
是(AmountType=10或AmountType=20)的快捷方式。
您尝试过搜索吗?我建议查找语法。AmountType IN(10,20)
是(AmountType=10或AmountType=20)的快捷方式。
否决此答案的人能否提供一些反馈,说明原因?我没有否决投票-我不反对否决投票,尽管我不会否决它,因为这个答案将返回正确的结果。您有2个选项,而不是1个不必要的选项。这两个选项的执行计划会比其他答案更糟糕。我不反对投票人。但我有个问题。那么,这个查询命中数据库的次数是多少?@IdontKnowEnglish这个查询命中数据库一次。只是不同的执行计划我不知道执行计划但是好的。。问题2)<代码>这可以提高性能
在什么基础上。。。?我想试试我自己。否决这个答案的人能提供一些反馈吗?我没有否决——我不反对否决票,尽管我不会否决,因为这个答案会返回正确的结果。您有2个选项,而不是1个不必要的选项。这两个选项的执行计划会比其他答案更糟糕。我不反对投票人。但我有个问题。那么,这个查询命中数据库的次数是多少?@IdontKnowEnglish这个查询命中数据库一次。只是不同的执行计划我不知道执行计划但是好的。。问题2)<代码>这可以提高性能
在什么基础上。。。?我想试试我自己。