日期:2014-05-18  浏览次数:20513 次

根据一个表的字段内容查询?????
A:表
--------------------------------
type   |   userid   |   text
------------------------------

B:表
----------------------------------
userid   |   name
----------------------------------

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

请教!!!!!!!!!!

------解决方案--------------------
上边的错了,呵呵

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 '代替



------解决方案--------------------
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