首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

根据一个表的字段内容查询?解决方法

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

根据一个表的字段内容查询?????
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

热点排行