读书人

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

发布时间: 2012-02-09 18:22:27 作者: rapoo

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

读书人网 >SQL Server

热点推荐