一个贴子,我刚写完代码想回复,发现贴子没了,真怪了,是哪个贴子呀,直结贴上代码及结果吧。要不白写了

hui_hui_2007 2008-03-28 01:25:10
一个贴子,我刚写完代码想回复,发现贴子没了,真怪了,是哪个贴子呀,直结贴上代码及结果吧。要不白写了

create table HouseInfo
(
id int,
chargetype int,
wuyetype int
)

insert into HouseInfo
values(1,2,3)

select *
from HouseInfo


create table ChargeType
(
id int,
name varchar(50)
)

insert into ChargeType
select 1 , '每月付款' union all
select 2 , '每季付款' union all
select 3 , '每年付款'

select * from ChargeType

create table WuYeType
(
id int,
name varchar(50)
)

insert into WuYeType
select 1,'住房' union all
select 2, '写字楼' union all
select 3 , '铺面' union all
select 4 , '厂房'

select * from WuYeType

select a.id,a.[name],c.[name]
from WuYeType a left join HouseInfo b
on a.id=b.wuyetype
left join ChargeType c on b.wuyetype=c.id

--结果:
/*
id name name
----------- -------------------------------------------------- --------------------------------------------------
1 住房 NULL
2 写字楼 NULL
3 铺面 每年付款
4 厂房 NULL

(所影响的行数为 4 行)

*/

不好意思,我想回的贴子实在找不到,好象最现论坛在升级吧。
...全文
73 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bjzhougm 2009-03-11
  • 打赏
  • 举报
回复
真是热心人
hui_hui_2007 2008-03-28
  • 打赏
  • 举报
回复
不好意思,有点错误,下面更正:

create table HouseInfo
(
id int,
chargetype int,
wuyetype int
)

insert into HouseInfo
values(1,2,3)

select *
from HouseInfo


create table ChargeType
(
id int,
name varchar(50)
)

insert into ChargeType
select 1 , '每月付款' union all
select 2 , '每季付款' union all
select 3 , '每年付款'

select * from ChargeType

create table WuYeType
(
id int,
name varchar(50)
)

insert into WuYeType
select 1,'住房' union all
select 2, '写字楼' union all
select 3 , '铺面' union all
select 4 , '厂房'

select a.id,a.name,c.name
from WuYeType a left join HouseInfo b
on a.id=b.wuyetype
left join ChargeType c on b.ChargeType=c.id

--结果:
/*
id name name
----------- -------------------------------------------------- --------------------------------------------------
1 住房 NULL
2 写字楼 NULL
3 铺面 每季付款
4 厂房 NULL

(所影响的行数为 4 行)
*/
dobear_0922 2008-03-28
  • 打赏
  • 举报
回复
支持

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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