查询结果显示格式的问题!

weinaxxc 2009-01-05 03:15:06
-----------------Table1----------------
cid cname
1 a
2 b
3 c
-----------------Table2----------------
id name cid
1 1111 1
2 2222 2
3 3333 3
4 4444 2

两表cid对应


------------------查询显示结果--------------------
id name
0 a
1 1111
0 b
2 2222
4 4444
0 c
3 3333
...全文
92 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
MJunnnn 2009-01-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 josy 的回复:]
SQL code---测试数据---
if object_id('[Table1]') is not null drop table [Table1]
go
create table [Table1]([cid] int,[cname] varchar(1))
insert [Table1]
select 1,'a' union all
select 2,'b' union all
select 3,'c'
if object_id('[Table2]') is not null drop table [Table2]
go
create table [Table2]([id] int,[name] varchar(10),[cid] int)
insert [Table2]
select 1,1111,1 union all
select 2,222…
[/Quote]
object_id('[Table2]')
这是一个什么函数啊..
wsh236 2009-01-10
  • 打赏
  • 举报
回复
都已经回答了,结贴吧!
dawugui 2009-01-10
  • 打赏
  • 举报
回复
create table Table1(cid int,cname varchar(1))
insert Table1
select 1,'a' union all
select 2,'b' union all
select 3,'c'
if object_id('Table2') is not null drop table Table2
go
create table Table2(id int,name varchar(10),cid int)
insert Table2
select 1,1111,1 union all
select 2,2222,2 union all
select 3,3333,3 union all
select 4,4444,2

select id , cname from
(
select id = 0 , cname , cid from table1
union all
select id ,cname = ' ' + name , cid from table2
) t
order by cid

drop table table1, table2

/*
id cname
----------- ------------
0 a
1 1111
2 2222
0 b
4 4444
0 c
3 3333

(所影响的行数为 7 行)
*/
bbb332 2009-01-10
  • 打赏
  • 举报
回复
xx
fcuandy 2009-01-05
  • 打赏
  • 举报
回复
order by cid,id
百年树人 2009-01-05
  • 打赏
  • 举报
回复
刚才顺序有点不对

---查询---
select id,cname from (
select 0 as id,cname,cid from [Table1]
union all
select id,name,cid from [Table2])t
order by cid,id

---结果---
id cname
----------- ----------
0 a
1 1111
0 b
2 2222
4 4444
0 c
3 3333

(所影响的行数为 7 行)
-晴天 2009-01-05
  • 打赏
  • 举报
回复
select id,cname from (
select 0 as id,cname,cid from [Table1]
union all
select id,' '+name,cid from [Table2])t
order by cid
百年树人 2009-01-05
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[Table1]') is not null drop table [Table1]
go
create table [Table1]([cid] int,[cname] varchar(1))
insert [Table1]
select 1,'a' union all
select 2,'b' union all
select 3,'c'
if object_id('[Table2]') is not null drop table [Table2]
go
create table [Table2]([id] int,[name] varchar(10),[cid] int)
insert [Table2]
select 1,1111,1 union all
select 2,2222,2 union all
select 3,3333,3 union all
select 4,4444,2

---查询---
select id,cname from (
select 0 as id,cname,cid from [Table1]
union all
select id,name,cid from [Table2])t
order by cid

---结果---
id cname
----------- ----------
0 a
1 1111
2 2222
0 b
4 4444
0 c
3 3333

(所影响的行数为 7 行)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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