sql语句?

Ice_wang 2006-11-27 03:11:15
firsttable:
ID ClientName ClientType ClientAddress
1 w 1 1
2 h 3 1
3 a 3 2
4 g 4 4
secondTable:
ID code text
A01 1 大客户
A01 2 一般
A01 3 差
A01 4 非常差
A02 1 北京
A02 2 上海
A02 3 广州
A02 4 深圳

求:
ID ClientName Clientype(对应的text值) clientAddress(对应的text值)
谢谢。。。急。。。
...全文
166 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
marco08 2006-11-27
  • 打赏
  • 举报
回复
create table firsttable(ID int, ClientName char(1), ClientType int, ClientAddress int)
insert firsttable select
1, 'w', 1, 1
union all select 2, 'h', 3, 1
union all select 3, 'a', 3, 2
union all select 4, 'g', 4, 4

create table secondTable(ID char(3), code int, [text] nvarchar(10))
insert secondTable select 'A01', 1, '大客户'
union all select 'A01', 2, '一般'
union all select 'A01', 3, '差'
union all select 'A01', 4, '非常差'
union all select 'A02', 1, '北京'
union all select 'A02', 2, '上海'
union all select 'A02', 3, '广州'
union all select 'A02', 4, '深圳'

select firstTable.ID, ClientName,
ClientType, ClientType_Text=(select [text] from secondTable where code=ClientType and ID='A01'),
ClientAddress, ClientAddress_Text=(select [text] from secondTable where code=ClientAddress and ID='A02')
from firstTable

--result
ID ClientName ClientType ClientType_Text ClientAddress ClientAddress_Text
----------- ---------- ----------- --------------- ------------- ------------------
1 w 1 大客户 1 北京
2 h 3 差 1 北京
3 a 3 差 2 上海
4 g 4 非常差 4 深圳

(4 row(s) affected)
子陌红尘 2006-11-27
  • 打赏
  • 举报
回复
select
ID,
ClientName,
(select [text] from secondTable where code=f.Clientype and ID='A01') as Clientype,
(select [text] from secondTable where code=f.ClientAddress and ID='A02') as ClientAddress
from
firsttable f
子陌红尘 2006-11-27
  • 打赏
  • 举报
回复
select
ID,
ClientName,
(select [text] from secondTable where code=f.Clientype and ID='A01') as Clientype,
(select [text] from secondTable where code=f.ClientAddress and ID='A02') as ClientAddress,
from
firsttable f

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧