sql 判断

FenHui 2008-11-25 12:37:47
一个范围判断的问题

如果a和b都不为空的话 则取出c值在a于b之间的数据
这个用between a and b 可以做
但是如果b为空的话 我想要的c值大于a就可以 (相当于b为最大值)

请教下这要怎么做啊
用一条sql语句哦


...全文
82 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2008-11-25
  • 打赏
  • 举报
回复
在存储过程中判断
declare @s varchar(200)
set @s='select * from table where 1=1'
if(A='' and b='')
set @s=@s+' and c between a and b'if(A='' and b='')
if(A!='' and b='')
set @s=@s+' and c > a '
exec @s
liudanking 2008-11-25
  • 打赏
  • 举报
回复
支持ls 加条件判断就可以了
孤独侠客123 2008-11-25
  • 打赏
  • 举报
回复
select c
from yourtable where (a is not null and b is not null and c>= a) or (b is null and c>=a)
孤独侠客123 2008-11-25
  • 打赏
  • 举报
回复
select (case when a is not null and b is not null and c>= a and c<= b then
c
when b is null and c>=a then c end ) from yourtable
dengchenlu 2008-11-25
  • 打赏
  • 举报
回复
用sql连接

string sql = "select * from tablename where 1 = 1"
if(a != "")
{
sql = sql + " and c > a";
}
if(b != "")
{
sql = sql + " and c <= a";
}
jinjazz 2008-11-25
  • 打赏
  • 举报
回复
between a and isnull(b,很大的数)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧