日期:2014-05-17  浏览次数:20477 次

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
请求解。谢谢。
SQL

------解决方案--------------------
select ID,classID,a_isPass,b_isPass,c_isPass from A where (classID=1 and a_isPass=1) or (classID= 2 and b_isPass=1) or (classID=3 and c_isPass=1)

这个不难啊
------解决方案--------------------

答案同1楼,这分也太容易挣了吧
------解决方案--------------------
exec sp_executesql
动态执行sql语句。
自定义你的sql以及的条件
------解决方案--------------------
引用:
用case when then的呢?

lz对于case when 不熟悉吗? case when 也比较简单
------解决方案--------------------
select ID,classID,a_isPass,b_isPass,c_isPass from A where classID in(1,2,3) and a_isPass=1

------解决方案--------------------
case when then 只能用操作classid的字段呀,不能从这个字段到另一个字段吧。。。
------解决方案--------------------
感觉不是查询是修改
------解决方案--------------------
update a
set a_isPass =
(case when classid = 1 then 1 else 0 end),
b_isPass =
(case when classid = 2 then 1 else 0 end),
c_isPass =
(case when classid = 3 then 1 else 0 end)
------解决方案--------------------
樓主是要這個麼
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