有2张表
A
PID CID
1 11
1 12
1 13
2 21
2 22
2 23B
ID
1
2
3
请问如何通过一条SQL语句得到C11
12
13
21
22
23
1
2
3
A
PID CID
1 11
1 12
1 13
2 21
2 22
2 23B
ID
1
2
3
请问如何通过一条SQL语句得到C11
12
13
21
22
23
1
2
3
解决方案 »
- Microsoft.Jet.OLEDB.4.0
- 用查询分析器连接异地服务器的错误提示
- 我还原了一个数据库,有表,但是引用就提示对象(表名)不可用。。。
- 连接问题~~~按照前面发的帖子都做过了,可是没用啊~~~哪位高手能解决?
- SQL 7.0 如何 验证 MDF文件?
- 求一些基本的SQL SERVER语句,谢谢,今天晚上结
- 求教SQL的update语句!急
- 最新的SQLServer2000/7.0的ODBC驱动的版本号是多少?
- 菜鸟的疑惑:“DBCC SHRINKDB”是什么意思,英文全称?它具体是干什么用的?
- ms sqlserver 2000 怎样才能解决以下问题
- 两个数据表关联
- MS SQL SERVER 2005 如何把数据库文件备份成DAT格式的啊
union all
select id from b
union all
select ID from B
union all
select ID c from B
---------------------------
select CID from A
union all--union all(包含重复数据),union(不包含重复数据)
select ID from B
select CID from A
union all
select ID as CID from B
declare @B table (id int , b varchar(10))
insert into @B
select 1,'a' union all
select 2,'b'
declare @C table (c varchar(10))
insert into @C
select 'c'select b as f from @B
union
select c as f from @C
/*
a
b
c
*/
A
PID CID
1 11
1 12
1 13
2 21
2 22
2 23
4 41
4 42B
ID
1
2
3
要得到C 11
12
13
21
22
23
1
2
3
from
(
select A.cid,'A'+cast(cid as varchar(10)) as orderCol
from A
union all
select B.id,'B'+cast(id as varchar(10)) as orderCol
from
)B
) AA
order by orderCol
union all
select ID c from B
from
(
select A.cid,'A'+cast(cid as varchar(10)) as orderCol
from A
where A.pid in
(select ID from B )
union all
select B.id,'B'+cast(id as varchar(10)) as orderCol
from B
) A
order by orderCol
from
(
select A.cid,'A'+cast(cid as varchar(10)) as orderCol
from A
where A.pid in
(select ID from B )
union all
select B.id,'B'+cast(id as varchar(10)) as orderCol
from B
) A
order by orderCol
/*
有2张表
A
PID CID
1 11
1 12
1 13
2 21
2 22
2 23 B
ID
1
2
3
请问如何通过一条SQL语句得到 C 11
12
13
21
22
23
1
2
3
*/create table #t4 (pid int,cid int)
create table #t5 (id int)insert into #t4 values ('1','11')
insert into #t4 values ('1','12')
insert into #t4 values ('1','13')
insert into #t4 values ('2','21')
insert into #t4 values ('2','22')
insert into #t4 values ('2','23')insert into #t5 values ('1')
insert into #t5 values ('2')
insert into #t5 values ('3')select cid from #t4
union all
select id from #t5
cid
-----------
11
12
13
21
22
23
1
2
3(所影响的行数为 9 行)
就是用UNION ALL来的啊
上面的没错吧
from (select a.cid cid,b.id id from b b left join a a on a.pid = b.id ) c
union all
select distinct c.id
from (select a.cid cid,b.id id from b b left join a a on a.pid = b.id ) c