日期:2014-05-18  浏览次数:20654 次

SQL CASE能否查询多个字段?
select price,(select count(*) from table1 where 
 CASE 
WHEN Identify=152 THEN aa='sss' 
ELSE bb='ssss' 
END
)num from dbo.table2

当table1表中的Identify=152条件时,查询aa='sss',否则查询bb='ssss'。求解?


------解决方案--------------------
SQL code
SELECT  price ,
        CASE WHEN EXISTS ( SELECT   1
                           FROM     table1
                           WHERE    Identify = 152 ) THEN ( SELECT
                                                              COUNT(*)
                                                            FROM
                                                              table1
                                                            WHERE
                                                              aa = 'sss'
                                                          )
             ELSE ( SELECT  COUNT(*)
                    FROM    table1
                    WHERE   bb = 'sss'
                  )
        END
FROM    table2