表a:
time input
08:55 aaa
09:30 bbb表b:
time output
12:05 ccc
23:11 ddd现想得到一合并的集合,即:TIME1 INPUT1 OUTPUT1
08:55 aaa
09:30 bbb
12:05 ccc
23:11 ddd用select a.time TIME1,input INPUT1,'' OUTPUT1 from a union select b.time TIME1,'' IMPUT1,output OUTPUT1 from b order by TIME1可是报错说两个select集类型不同,貌似''与a.input及b.output长度不同(虽然都是字符型)
那么,我该怎么解决呢,谢谢
time input
08:55 aaa
09:30 bbb表b:
time output
12:05 ccc
23:11 ddd现想得到一合并的集合,即:TIME1 INPUT1 OUTPUT1
08:55 aaa
09:30 bbb
12:05 ccc
23:11 ddd用select a.time TIME1,input INPUT1,'' OUTPUT1 from a union select b.time TIME1,'' IMPUT1,output OUTPUT1 from b order by TIME1可是报错说两个select集类型不同,貌似''与a.input及b.output长度不同(虽然都是字符型)
那么,我该怎么解决呢,谢谢
就是拿NULL代替''
(
time varchar(10),
input varchar(10)
)
insert into a
select '08:55','aaa' union all
select '09:30', 'bbb'
select * from a
create table b
(
time varchar(10),
output varchar(10)
)
insert into b
select '12:05', 'ccc' union all
select '23:11', 'ddd'
select * from bselect a.time TIME1,input INPUT1,'' OUTPUT1 from a union select b.time TIME1,'' IMPUT1,output OUTPUT1 from b order by TIME1楼主是SQl SERver 2000么?
union
select b.time TIME1,
cast('' as A表的input列的数据类型),
cast(output as A表的output列的数据类型)
from b order by TIME1
union select b.time TIME1,'' INPUT1,output OUTPUT1 from b order by TIME1
这个报错的话,很可能就是两个集合的某些字段长度不同造成的可以这样解决
建立一个临时表,把其字段名、类型、长度等确定
再把两集合都插入此临时表