select studentid,value=(select case when count(*)=0 then 0 else 1 end from log where student.studentid=studentid) from student

解决方案 »

  1.   

    select 
         a.studentid ,
         case when b.studentid is null then 0 else 1 end as value
    from 
         student a
    left join
         (select distinct studentid from log) b
    on 
         a.studentid = b.studentid
      

  2.   


    select a.studentid,case when (select 1 from log where studentid=a.studentid)=1 then 1 else 0 end as value from student as a
      

  3.   

    select studentid,(select case when count(1)=0 then 0 else 1 end from log where student.studentid=studentid) from student
      

  4.   

    select id,value=(select case when count(*)=0 then 0 else 1 end from log where stu.id=log.stuid)
       from stu
      

  5.   

    select *
    ,case when exists (select 1 from log where studentid = student.studentid)
           then 1
          else
             0
      end
    from student
      

  6.   

    select *
    ,case when (select count(*) from log where studentid = student.studentid) > 0
           then 1
          else
             0
      end
    from student
      

  7.   

    select studentid,value=(select case when count(*)=0 then 0 else 1 end from log where student.studentid=studentid) from student
      

  8.   

    declare @student Table(studentid int)
    insert into @student values(1)
    insert into @student values(2)
    insert into @student values(3)
    insert into @student values(4)
    declare @log Table(id int,studentid int,bookname varchar(50))
    insert into @log values(1,1,'book1')
    insert into @log values(2,2,'book2')
    insert into @log values(3,2,'book3')
    insert into @log values(4,2,'book3')
    insert into @log values(5,1,'book2')
    insert into @log values(6,2,'book2')select a.studentid,
    (case 
    when a.studentid in (select b.studentid from @log b) then 1
    else 0
    End) as value 
    from @student a