电影程序:一个film_class表
id,classname 两个字段。如:
1   动作
2   喜剧
3   科幻
4   恐怖
5   言情一个sub表。
大概内容是(就是某个用户订阅了某个类别)
userid classid
1       2
1       4
2       1
2       5
现在要更具userid得到结果如userid =1的时候如下:id,classname ,is_sub
1   动作        false
2   喜剧        true
3   科幻        false
4   恐怖        true
5   言情        false

解决方案 »

  1.   


    --try select a.* ,case when userid is null then [false] else [true] end 
    from film_class  a
    left join sub on a.id =b.userid
    where userid =1
      

  2.   

    select A.*,case when B.classid is null then 'False' else 'True' end as is_sub
    from film_class A left join (select * from sub where userid =1) B on A.ID=B.classid
      

  3.   


    --try 
    create table film_class
    (
    id int identity (1,1),
    classname varchar(10)
    )create table sub
    (
    userid int ,
    classid int ,
    )insert into film_class 
    select '动作' union all 
    select '喜剧' union all 
    select '科幻' union all 
    select '恐怖' union all 
    select '言情' insert into sub 
    select 1  ,     2 union all 
    select 1 ,      4 union all 
    select 2 ,      1 union all 
    select 2 ,      5select a.* ,case when b.userid is null then 'false' else 'true' end 
    from film_class  a
    left join (select * from sub where userid =1) b on a.id =b.classid   
    id          classname        
    ----------- ---------- ----- 
    1           动作         false
    2           喜剧         true
    3           科幻         false
    4           恐怖         true
    5           言情         false(所影响的行数为 5 行)