请高手帮小弟一下!

petre 2003-10-10 09:52:12
我建了两个表:
industry 字段为:
code name
1 机械
2 电子
3 化工
product 字段为:
code productname
1 水泵
1 变压器
2 晶体管
2 导体
2 电子板
3 纸浆
4 科普书
4 社会学
我将两表关联建立一个视图 q_product
为什么只有显示为: name productname
机械 水泵
机械 变压器
电子 晶体管
电子 导体
电子 电子板
化工 纸浆

如何显示为:
name productname
机械 水泵
机械 变压器
电子 晶体管
电子 导体
电子 电子板
化工 纸浆
null 科普书
null 社会学

或者如何将
4 科普书
4 社会学
这些记录从product中分离出来
谢谢各位高手了!在线等待!

...全文
31 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhbname 2003-10-10
  • 打赏
  • 举报
回复
up
welyngj 2003-10-10
  • 打赏
  • 举报
回复
1.select [name],productname from industry i right join product p on i.[name]=p.[name]
2select [name],productname from industry i,product p where [name] not in ( select [name] from industry i,product p where i.[name]=p.[name])
zjcxc 元老 2003-10-10
  • 打赏
  • 举报
回复
我的字段名写错了一此,改:

--得到查询结果
select a.name,b.productname
from industry a right join product b on a.code=b.code

--得到分离结果
select * from product where code not in (select code from industry)


--下面是数据测试
declare @industry table(code int,name varchar(10))
insert into @industry
select 1,'机械'
union all select 2,'电子'
union all select 3,'化工'
declare @product table(code int,productname varchar(10))
insert into @product
select 1,'水泵'
union all select 1,'变压器'
union all select 2,'晶体管'
union all select 2,'导体'
union all select 2,'电子板'
union all select 3,'纸浆'
union all select 4,'科普书'
union all select 4,'社会学'

select a.name,b.productname
from @industry a right join @product b on a.code=b.code

select * from @product where code not in (select code from @industry)
txlicenhe 2003-10-10
  • 打赏
  • 举报
回复
测试:
create table industry(code int,[name] varchar(10))
insert industry select 1,'机械'
union all select 2,'电子'
union all select 3,'化工'

create table product(code int,productname varchar(10))
insert product select 1,'水泵'
union all select 1,'变压器'
union all select 2,'晶体管'
union all select 2,'导体'
union all select 2,'电子板'
union all select 3,'纸浆'
union all select 4,'科普书'
union all select 4,'社会学'


Select b.[name],a.productname from product a
left join industry b on a.code = b.code

name productname
---------- -----------
机械 水泵
机械 变压器
电子 晶体管
电子 导体
电子 电子板
化工 纸浆
NULL 科普书
NULL 社会学

(所影响的行数为 8 行)

Select a.code,a.productname from product a
left join industry b on a.code = b.code
where b.code is null

code productname
----------- -----------
4 科普书
4 社会学

(所影响的行数为 2 行)

pengdali 2003-10-10
  • 打赏
  • 举报
回复
select a.name,b.productname from industry a right join product b on a.code=b.code
伍子V5 2003-10-10
  • 打赏
  • 举报
回复
Select a.[name],b.productname from industry a
right join product b on a.code = b.code
zjcxc 元老 2003-10-10
  • 打赏
  • 举报
回复
分离出来就用:

select * from product where colde not in (select code from industry)
zjcxc 元老 2003-10-10
  • 打赏
  • 举报
回复
select a.name,b.name
from industry a right join product b on a.code=b.code
txlicenhe 2003-10-10
  • 打赏
  • 举报
回复
1:或:
Select b.[name],a.productname from product a
left join industry b on a.code = b.code
2:
Select a.code,a.productname from product a
left join industry b on a.code = b.code
where b.code is null



txlicenhe 2003-10-10
  • 打赏
  • 举报
回复
Select a.[name],b.productname from industry a
right join product b on a.code = b.code

34,590

社区成员

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

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