100分,求sql语句,带选择条件
如下:
表名: A
字段名:ID,classID,a_isPass,b_isPass,c_isPass;
现在要查询条件是:当classID = 1时 a_isPass =1,当当classID = 2时 b_isPass =1,当classID = 3时 c_isPass =1
请求解。谢谢。
答案同1楼,这分也太容易挣了吧
[解决办法]
exec sp_executesql
动态执行sql语句。
自定义你的sql以及的条件
[解决办法]
select ID,classID,a_isPass,b_isPass,c_isPass from A where classID in(1,2,3) and a_isPass=1
SELECT
ID,
CLASSID,
(CASE CLASSID=1 THEN 1 ELSE A_ISPASS END) AS A_ISPASS,
(CASE CLASSID=2 THEN 1 ELSE B_ISPASS END) AS B_ISPASS,
(CASE CLASSID=3 THEN 1 ELSE C_ISPASS END) AS C_ISPASS
/*--测试数据 BY Flyinsky1
create table #tb(tID int,
classID int,
a_isPass int,
b_isPass int,
c_isPass int)
insert into #tb
select 1,1,1,0,0
union all
select 2,2,0,1,0
union all
select 3,3,0,1,1
union all
select 4,2,1,1,0
*/
--解法1
select tID,classID,'pass'=case when classid=1 and a_ispass=1 then 'a_ispass'
when classid=2 and b_ispass=1 then 'b_ispass'
when classid=3 and c_ispass=1 then 'c_ispass'else '' end
from #tb
--解法2
select tID,ClassID =case when classID=1 then 1 when classID=2 then 2
when classID=3 then 3 else 0 end,
a_ispass = case when a_ispass=1 then 1 else 0 end,
b_ispass=case when b_ispass=1 then 1 else 0 end,
c_ispass=case when c_ispass=1 then 1 else 0 end
from #tb