select  case when ys is null then 1 else  ys  end from yfmx where ypid=23423从表yfmx中取字段ys的数据,当没有符合where条件时,返回1,有符合的返回最后一条ys值
怎样写?

解决方案 »

  1.   

    用order by取最后一条,如果没有返回1可以写在asp.net的判断语句里,没必要这么麻烦
      

  2.   

    select case Count(*) when 0 then '1' else ys end as ys from yfmx where ypid=23423
      

  3.   


    select case when (select count(*) from from yfmx where ypid=23423) is null then 1 else ys end from yfmx
      

  4.   

    看看这个行不?如有错误还请原谅(没有经过测试)
    select
    case when (select count(*) from yfmx where ypid=23423)>0
    then
    1
    else
    ys
    end
    from yfmx where ypid=23423
      

  5.   

    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' 附近有语法错误。 
      

  6.   

    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 后面更改
      

  7.   

    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 行)