求一sql语句
admin 发表于 2010-04-21 | 来源:互联网 | 阅读:
select case when ys is null then 1 else ys end from yfmx where ypid=23423
从表yfmx中取字段ys的数据,当没有符合where条件时,返回1,有符合的返回最后一条ys值
怎样写?
select case when ys is null then 1 else ys end from yfmx where ypid=23423
从表yfmx中取字段ys的数据,当没有符合where条件时,返回1,有符合的返回最后一条ys值
怎样写?
评论功能因故关闭!
请加入我们的QQ群一起参与讨论:群号59400482(500人超级群)
用order by取最后一条,如果没有返回1可以写在asp.net的判断语句里,没必要这么麻烦
select case Count(*) when 0 then ‘1′ else ys end as ys from yfmx where ypid=23423
select case when (select count(*) from from yfmx where ypid=23423) is null then 1 else ys end from yfmx
看看这个行不?如有错误还请原谅(没有经过测试)selectcase when (select count(*) from yfmx where ypid=23423)>0then1elseysendfrom yfmx where ypid=23423
SQL code
select case when max(ye) is null then 0 else max(ye) end from (select top 1 ye from yfmx where ypid=2 and ksbh=050104 order by id desc) a
我也写了一条,可以返回需要的值,因为此返回值将作为一个值插入一条数据中,当这句写在sql之中时就错了:服务器: 消息 156,级别 15,状态 1,行 4在关键字 ’select’ 附近有语法错误。
SQL code
SELECT ISNULL(
(SELECT TOP 1 LTRIM(ys) from yfmx where ypid=23423 ORDER BY YS DESC),’1′)
楼主YS如果是INT型,后面的’1′可改为1前面的LTRIM(YS)不用加LTRIM,而且你要取最后一条,得按什么排序,在ORDER BY 后面更改
SQL code
DECLARE @yfmx TABLE(YS INT,YPID INT)
INSERT @yfmx
SELECT 2,23423
UNION ALL
SELECT 3,23423
SELECT ISNULL(
(SELECT TOP 1 ys from @yfmx where ypid=23423 ORDER BY YS DESC),1)
(所影响的行数为 2 行)
———–
3
(所影响的行数为 1 行)
解决,谢谢。。。。。。。。。。