表一
id 名称 型号
1 1 1
4 4 4
表二
id 质量 尺寸
1 1 1
2 2 2
3 3 3
表三
id 名称 型号 质量 尺寸
1 1 1 1 1
2 2 2
3 3 3
4 4 4
请问用什么语句检索得到第三个表
id 名称 型号
1 1 1
4 4 4
表二
id 质量 尺寸
1 1 1
2 2 2
3 3 3
表三
id 名称 型号 质量 尺寸
1 1 1 1 1
2 2 2
3 3 3
4 4 4
请问用什么语句检索得到第三个表
解决方案 »
- 求 求职简历 一份
- @dbname.dbo.tablename 出错,库名如何引用变量得到
- 关于 zjcxc(邹建)大哥 分页存储过程(p_show)的分页提问
- execute @strSql为什么不能执行啊
- sql 数据类型转换问题
- LTRIM与RTRIM的区别
- 大量的login failed for user sa 错误请教
- 上传二进制文件到数据库的问题,答者有分(急,在线等)
- 这段代码能不能简化?
- 為甚麼有時得到的Recordset明明有紀錄,但它的recordcount屬性卻是 -1 ?? 那位解釋一下(longjj)
- 并发查询时tempdb增大比较快
- 扫描出有其它IP访问我的数据库怎么办?
想取个最大集合,用union又不好合并相同id的
id 名称 型号
1 1 1
4 4 4
表二
id 质量 尺寸
1 1 1
2 2 2
3 3 3
表三
id 质量2 尺寸2
5 5 5
6 6 6
4 42 42
表三
id 名称 型号 质量 尺寸 质量2 尺寸2
1 1 1 1 1
2 2 2
3 3 3
4 4 4 42 42
5 5 5
6 6 6
union
select id, 质量 as 名称, 尺寸 as 型号,'','' from 表二 where id not in (select id from 表一)
go
insert 表一 select 1, '1', '1'
insert 表一 select 4, '4', '4'
create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
go
insert 表二 select 1, '1', '1'
insert 表二 select 2, '2', '2'
insert 表二 select 3 ,'3', '3'
go
select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸 From 表一 a left join 表二 b on a.id=b.id
union
select id, 质量 as 名称, 尺寸 as 型号,'','' from 表二 where id not in (select id from 表一)drop table 表一,表二
go
/*
id 名称 型号 质量 尺寸
----------- ---------- ---------- ---------- ----------
1 1 1 1 1
2 2 2
3 3 3
4 4 4 (所影响的行数为 4 行)*/
insert into tb1 values (1,'1','1')
insert into tb1 values (4,'4','4')create table tb2(id int,质量 varchar(20),尺寸 varchar(20))
insert into tb2 values (1,'1','1')
insert into tb2 values (2,'2','2')
insert into tb2 values (3,'3','3')
create table tb3(id int,质量2 varchar(20),尺寸2 varchar(20))
insert into tb3 values (5,'1','5')
insert into tb3 values (6,'6','6')
insert into tb3 values (4,'42','42')
select tb2.id,名称,型号,isnull(tb2.质量,'') 质量,isnull(tb2.尺寸,'') 尺寸,'' as 质量2,''尺寸2 from tb2 left join tb1 on tb1.id=tb2.id
union
select tb3.id,名称,型号,'','',isnull(tb3.质量2,'') 质量2,isnull(tb3.尺寸2,'') 尺寸2 from tb3 left join tb1 on tb1.id=tb3.id
create table 表一 (id int ,名称 varchar(10),型号 varchar(10) )
go
insert 表一 select 1, '1', '1'
insert 表一 select 4, '4', '4'
create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
go
insert 表二 select 1, '1', '1'
insert 表二 select 2, '2', '2'
insert 表二 select 3 ,'3', '3'
go
select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸 From 表一 a left join 表二 b on a.id=b.id
union
select id, '','',质量 , 尺寸 from 表二 where id not in (select id from 表一)drop table 表一,表二
go
/*
id 名称 型号 质量 尺寸
----------- ---------- ---------- ---------- ----------
1 1 1 1 1
2 2 2
3 3 3
4 4 4 (所影响的行数为 4 行)
*/
union
select tb3.id,isnull(名称,''),isnull(型号,''),'','',isnull(tb3.质量2,'') 质量2,isnull(tb3.尺寸2,'') 尺寸2 from tb3 left join tb1 on tb1.id=tb3.id
go
insert 表一 select 1, '1', '1'
insert 表一 select 4, '4', '4'
create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
go
insert 表二 select 1, '1', '1'
insert 表二 select 2, '2', '2'
insert 表二 select 3 ,'3', '3'
go
create table 表三(id int, 质量2 varchar(10), 尺寸2 varchar(10))
go
insert 表三 select 5, '5' , '5'
insert 表三 select 6, '6' , '6'
insert 表三 select 4, '42', '42'
go
select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸,'' 质量2,'' 尺寸2 From 表一 a left join 表二 b on a.id=b.id
union
select id, '','',质量 , 尺寸,'' 质量2,'' 尺寸2 from 表二 where id not in (select id from 表一)
union
select id ,'','','','',质量2,尺寸2 from 表三
drop table 表一,表二,表三
go
/*
id 名称 型号 质量 尺寸 质量2 尺寸2
----------- ---------- ---------- ---------- ---------- ---------- ----------
1 1 1 1 1
2 2 2
3 3 3
4 42 42
4 4 4
5 5 5
6 6 6
*/
go
insert 表一 select 1, '1', '1'
insert 表一 select 4, '4', '4'
create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
go
insert 表二 select 1, '1', '1'
insert 表二 select 2, '2', '2'
insert 表二 select 3 ,'3', '3'
go
create table 表三(id int, 质量2 varchar(10), 尺寸2 varchar(10))
go
insert 表三 select 5, '5' , '5'
insert 表三 select 6, '6' , '6'
insert 表三 select 4, '42', '42'
go
select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸,isnull(c.质量2,'') 质量2,isnull(c.尺寸2,'') 尺寸2 From 表一 a left join 表二 b on a.id=b.id left join 表三 c on a.id=c.id
union
select id, '','',质量 , 尺寸,'' 质量2,'' 尺寸2 from 表二 where id not in (select id from 表一)
union
select id ,'','','','',质量2,尺寸2 from 表三 where id not in (select id from 表一)
drop table 表一,表二,表三
go
/*
id 名称 型号 质量 尺寸 质量2 尺寸2
----------- ---------- ---------- ---------- ---------- ---------- ----------
1 1 1 1 1
2 2 2
3 3 3
4 4 4 42 42
5 5 5
6 6 6
*/
id 名称 型号
1 名称1 名称1
4 名称4 名称4表2
id 质量 尺寸
1 质量1 质量1
2 质量2 质量2
3 质量3 质量3表3
id 数量 体积
2 数量2 体积2
4 数量4 体积4表4
id 库存
2 库存2
5 库存5最终检索结果
id 名称 型号 质量 尺寸 数量 体积 库存
1 名称1 型号1 质量1 尺寸1
2 质量2 尺寸2 数量2 体积2 库存2
3 质量3 尺寸3
4 名称4 型号4 数量4 体积4
5 库存5
go
insert 表一 select 1, '1', '1'
insert 表一 select 4, '4', '4'
create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
go
insert 表二 select 1, '1', '1'
insert 表二 select 2, '2', '2'
insert 表二 select 3 ,'3', '3'
go
create table 表三(id int, 质量2 varchar(10), 尺寸2 varchar(10))
go
insert 表三 select 5, '5' , '5'
insert 表三 select 6, '6' , '6'
insert 表三 select 3, '42', '42'
go
select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸,isnull(c.质量2,'') 质量2,isnull(c.尺寸2,'') 尺寸2 From 表一 a left join 表二 b on a.id=b.id left join 表三 c on a.id=c.id
union
select id, '','',质量 , 尺寸,'' 质量2,'' 尺寸2 from 表二 where id not in (select id from 表一)
union
select id ,'','','','',质量2,尺寸2 from 表三 where id not in (select id from 表一)
drop table 表一,表二,表三
go
/*id 名称 型号 质量 尺寸 质量2 尺寸2
----------- ---------- ---------- ---------- ---------- ---------- ----------
1 1 1 1 1
2 2 2
3 42 42
3 3 3
4 4 4
5 5 5
6 6 6
*/
go
create table [ta]([id] int,[名称] int,[型号] int)
insert [ta]
select 1,1,1 union all
select 4,4,4
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[质量] int,[尺寸] int)
insert [tb]
select 1,1,1 union all
select 2,2,2 union all
select 3,3,3--select * from [ta]
--select * from [tb]select id=isnull(a.id,b.id),a.[名称],a.[型号],b.[质量],b.[尺寸]
from ta a full join tb b
on a.id=b.id
order by id
--测试结果:
/*
id 名称 型号 质量 尺寸
----------- ----------- ----------- ----------- -----------
1 1 1 1 1
2 NULL NULL 2 2
3 NULL NULL 3 3
4 4 4 NULL NULL(4 行受影响)
*/
create table 表一 (id int ,名称 varchar(10),型号 varchar(10) )
go
insert 表一 select 1, '1', '1'
insert 表一 select 4, '4', '4'
create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
go
insert 表二 select 1, '1', '1'
insert 表二 select 2, '2', '2'
insert 表二 select 3 ,'3', '3'
go
create table 表三(id int, 质量2 varchar(10), 尺寸2 varchar(10))
go
insert 表三 select 5, '5' , '5'
insert 表三 select 6, '6' , '6'
insert 表三 select 3, '42', '42'
go
select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸,isnull(c.质量2,'') 质量2,isnull(c.尺寸2,'') 尺寸2 From 表一 a left join 表二 b on a.id=b.id left join 表三 c on a.id=c.id
union
select id, '','',质量 , 尺寸,'' 质量2,'' 尺寸2 from 表二 where id not in (select id from 表一)union
select id ,'','','','',质量2,尺寸2 from 表三
where id not in (select id from 表二) and
id not in(select id from 表一) --改了下
drop table 表一,表二,表三
go
/*id 名称 型号 质量 尺寸 质量2 尺寸2
----------- ---------- ---------- ---------- ---------- ---------- ----------
1 1 1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
*/改了下,就好了
full join 可以这样就好了!
写完整点是: full outer join
外联的几种方式之一。
Access有没有这个full join
但是可以用变通的方法:先left join,再right join,然后把结果集合并。
SELECT table1.*, table2.*
FROM table1 LEFT JOIN table2
ON table1.id = table2.id
UNION --ALL
SELECT table1.*, table2.*
FROM table1 RIGHT JOIN table2
ON table1.id = table2.id
select distinct id into # from
(select id from t1
union all select id from t2
union all select id from t3
--union all...
) tselect #.id,t1.col1,t2.col2 --,...
from #
left join t1 on #.id=t1.id
left join t2 on #.id=t2.id
--...