有3个表:
tb_user:userid, name, pwd, info 
tb_app:appid,name,key   
tb_userapp:userappid,userid,appid前两个就是一般的表,第三个表是前两个的关系表,我想要是SQL语句是:
在appid确定的情况下列出所有用户userid name info isaccess这些列,但是isaccess是这几个表都没有的,我希望加上这一列,并且在关系表tb_userapp中存在则返回1,没有则返回0

解决方案 »

  1.   

    select (case when 存在 then 1 else 0 end) as isaccess from tb
      

  2.   

    这样的话我应该这样写:
    select userid,[name],realname,info,islocked,select (case when 存在 then 1 else 0 end) as isaccess  from tb_user
    但是存在这个SQL语句怎么写,该条user记录的userid不可以在表tb_app中使用吧
      

  3.   

    select A.userid,A.name,A.info ,(case when B.userid is null then 1 else 0 end)isaccess
    from tb_user A left join tb_userapp B on A.userid=B.userid
      

  4.   

    果然好使 忘记了还有个LEFT JOIN这个东西 谢谢了