跪求一条SQL语句
时间:2011-11-19
来源:互联网
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
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
select * from A union select * from B
作者: qgqch2008 发布时间: 2011-11-19
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
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
select * from ta intersect select * from tb
作者: Beirut 发布时间: 2011-11-19
不好意思,我想再问一个问题,
就是有一个表有很多列
比如:A表:列1,列2,....列100
我想除了列50,列60,列61,列99,这四列不返回,其他全返回请问怎么写阿
作者: WelcomeToCSDN 发布时间: 2011-11-19
谢谢qianjin036a的回答,问题可以解决了,
不好意思,我想再问一个问题,
就是有一个表有很多列
比如:A表:列1,列2,....列100
我想除了列50,列60,列61,列99,这四列不返回,其他全返回请问怎么写阿
没有捷径,得写全除了不返回的列以外的所有列名.
作者: qianjin036a 发布时间: 2011-11-19
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
--如果显示的是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
谢谢qianjin036a的回答,问题可以解决了,
不好意思,我想再问一个问题,
就是有一个表有很多列
比如:A表:列1,列2,....列100
我想除了列50,列60,列61,列99,这四列不返回,其他全返回请问怎么写阿
得到表中除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
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28