A:表
--------------------------------
type | userid | text
------------------------------B:表
----------------------------------
userid | name
----------------------------------查询A表 要的到的结果为
--------------------------------
type | userid | text | name
------------------------------
如果 A表的 type=1 就在B表中找userid相同的name列出来 
如果 A标的 type!=1 就 用一个字符串代替??请教!!!!!!!!!!

解决方案 »

  1.   

    select a.*,b.name from a join b on a.userid=b.userid
      

  2.   


    select a.*,b.name 
    from a ,b where  a.userid=b.userid
      

  3.   

    select type,a.userid,text,name from a join b on a.userid = b.userid where type = 1
    union all select type,a.userid,text,'aaa' from a如果 A表的 type=1 就在B表中找userid相同的name列出来
    如果 A标的 type!=1 就 用一个字符串'aaa'代替
      

  4.   

    上边的错了,呵呵select type,a.userid,text,name from a join b on a.userid = b.userid where type = 1
    union all select type,a.userid,text,'aaa' from a where type <> 1如果 A表的 type=1 就在B表中找userid相同的name列出来
    如果 A标的 type!=1 就 用一个字符串'aaa'代替
      

  5.   

    select type,userid=case type when 1 then a.userid else '字符串' end
    ,text,
    name=case type when 1 then b.name else '字符串' end
    from a ,b where  a.userid=b.userid
      

  6.   

    如果我要根据 type=2  变化 又 多一个 c表 和b表一样呢
      

  7.   

    如果变化很多,就用动态SQL解决