+ -
当前位置:首页 → 问答吧 → 跪求一条SQL语句

跪求一条SQL语句

时间:2011-11-19

来源:互联网

A表和B表结构相同

A表:
  id name time

B表
  id name time

两表记录数不同

现在A表通过id连接B表,如果A表中的id在B表中有,则返回A表中对应记录,如果没有则返回B表中记录。
最后生成结构相同的C表
C表
  id name time

作者: WelcomeToCSDN   发布时间: 2011-11-19

SQL code
select * from a where exists(select 1 from b where id=a.id)
union all
select * from b where not exists(select 1 from a where id=b.id)

作者: qianjin036a   发布时间: 2011-11-19

SQL code
select * from A
union
select * from B

作者: qgqch2008   发布时间: 2011-11-19

SQL code
select * from a 
union all
select * from b where not exists(select 1 from a where a.id=b.id)


如果在a表有在b表没有的不显示出来,则在第一条语句里加 where exists(select 1 from b where a.id=b.id)

作者: josy   发布时间: 2011-11-19

SQL code

if object_id('A','U') is not null
   drop table A
go
create table A
(
 id int,
 name varchar(10),
 time varchar(10)
)
go
insert into A
select 1,'张三','2011-10-10' union all
select 2,'李四','2011-11-11'
go
if object_id('B','U') is not null
   drop table B
go
create table B
(
 id int,
 name varchar(10),
 time varchar(10)
)
go
insert into B
select 1,'张三','2011-09-09' union all
select 3,'王五','2011-11-12'
go
select * from A t1 where exists(select 1 from B where id=t1.id)
union all
select * from B t1 where not exists(select 1 from A where id=t1.id)
go
/*
id          name       time
----------- ---------- ----------
1           张三         2011-10-10
3           王五         2011-11-12

(2 行受影响)
*/

作者: pengxuan   发布时间: 2011-11-19

SQL code
select * from ta
intersect
select * from tb

作者: Beirut   发布时间: 2011-11-19

谢谢qianjin036a的回答,问题可以解决了,
不好意思,我想再问一个问题,
就是有一个表有很多列
比如:A表:列1,列2,....列100
我想除了列50,列60,列61,列99,这四列不返回,其他全返回请问怎么写阿

作者: WelcomeToCSDN   发布时间: 2011-11-19

引用 6 楼 welcometocsdn 的回复:
谢谢qianjin036a的回答,问题可以解决了,
不好意思,我想再问一个问题,
就是有一个表有很多列
比如:A表:列1,列2,....列100
我想除了列50,列60,列61,列99,这四列不返回,其他全返回请问怎么写阿


没有捷径,得写全除了不返回的列以外的所有列名.

作者: qianjin036a   发布时间: 2011-11-19

--如果显示的是id ,name, time
select * from a
union all
select * from b where id not exists(select 1 from a where a.id = b.id)

作者: dawugui   发布时间: 2011-11-19

引用 8 楼 dawugui 的回复:
--如果显示的是id ,name, time
select * from a
union all
select * from b where id not exists(select 1 from a where a.id = b.id)

SQL code
--如果显示的是id ,name, time
select * from a
union all
select * from b where not exists(select 1 from a where a.id = b.id)

select * from a
union all
select * from b where id not in(select distinct id from a )

作者: dawugui   发布时间: 2011-11-19

引用 6 楼 welcometocsdn 的回复:
谢谢qianjin036a的回答,问题可以解决了,
不好意思,我想再问一个问题,
就是有一个表有很多列
比如:A表:列1,列2,....列100
我想除了列50,列60,列61,列99,这四列不返回,其他全返回请问怎么写阿
SQL code
得到表中除Col1、Col2的所有列

例如:userno_fm、userno_to
create table test(
       num int identity(1,1),
       userno_fm varchar(10),
       userno_to varchar(10),
       username varchar(10))
select * from test

declare @sql varchar(8000)
    select @sql=''
    select @sql=@sql+','+[name] from 
    (select [name] from syscolumns where object_id(N'[test]')=[id] and [name] not in ('userno_fm','userno_to')) A

    set @sql='select '+stuff(@sql,1,1,'')+' from [test]'
    --print @sql
    exec (@sql)

drop table test


select * from syscolumns where id=object_id('tableName') and name not in('a','b')

作者: dawugui   发布时间: 2011-11-19

热门下载

更多