求Sql语句,复制表

梅子 2011-07-11 10:32:09
现有表A结构一致,如下:
ID,主键,自增
Account varchar(50)
Session1 bit
Session2 bit
AccountID int
upTime datetime

A表模拟数据:
1 aaaa true false 123 2011-02-02 21:33:00
2 bbbb false true 234 2011-02-03 21:33:00
3 cccc ture false 345 2011-02-03 22:00:00

表B结构如下
Session1ID int
Session2ID int
Account varchar(5)
uptime datetime

B表模拟数据:
123 null aaa 2011-01-03 00:00:00
555 null ieie 2011-03-05 00:00:00
null 3332 i9wks 2011-03-06 00:00:00



现在要把表B中存在的记录复制到表A中
条件如下:
表A中原来也是有记录的,如果A中存在,则不插入,不存在才插入
具体情况为:
如果B中的Session1ID不为NULL,则比对A中Session1为True的Account是否存在
不存在就插入记录,存在就不插入

如果B中的Session2ID不为NUll,则比对A中Session2为True的Account是否存在
不存在就插入记录,存在就不插入

不知道说清楚没,请教大大们应该怎么做啊?
...全文
135 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
梅子 2011-07-12
  • 打赏
  • 举报
回复
我最后的Sql是:


INSERT INTO a
SELECT
account,
1,
0,
Session1ID,
getdate()
FROM
b
WHERE
account not in (
SELECT
b.account
FROM
b,a
WHERE
Session1ID is not null
and b.account=a.account
GROUP BY
b.account
)


贴一下,怕以后忘记。。谢谢大家,结贴
--小F-- 2011-07-11
  • 打赏
  • 举报
回复

if (b.Session1ID is not null)
insert into
a
select
*
from
b where
Session1ID is not null
and
not exists (select 1 from a where Session1=True and b.Session1ID=a.Account)

if(b.Session2ID is not null)
insert
a
select
*
from
b
where
Session2ID is not null
and
not exists (select 1 from a where Session2=True and b.Session2=a.Account)
sekai2011 2011-07-11
  • 打赏
  • 举报
回复
mark
梅子 2011-07-11
  • 打赏
  • 举报
回复
insert a
select * from b where ???? not exists (select 1 from a where Session1=True and b.Session1ID=a.Account and a.Session1ID is not null
)
chuanzhang5687 2011-07-11
  • 打赏
  • 举报
回复
那就看豆子哥写的[Quote=引用 9 楼 sunnxxy 的回复:]
引用 8 楼 chuanzhang5687 的回复:
这两个表那什么关联?


没有关联,只是存的数据比较类似,想合并到一起
[/Quote]
梅子 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chuanzhang5687 的回复:]
这两个表那什么关联?
[/Quote]

没有关联,只是存的数据比较类似,想合并到一起
chuanzhang5687 2011-07-11
  • 打赏
  • 举报
回复
这两个表那什么关联?
梅子 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cd731107 的回复:]
SQL code

--1.
insert a
select * from b where Session1ID is not null
and not exists (select 1 from a where Session1=True and b.Session1ID=a.Account)
[/Quote]

insert a
select * from b where Session1ID is not null
以上条件好像是正确的,但是not exists中好像没有比对出来,插入的时候好多为Null的数据也被插入了
hanger1212 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cd731107 的回复:]

SQL code
--2.
insert a
select * from b where Session2ID is not null
and not exists (select 1 from a where Session2=True and b.Session2=a.Account)
[/Quote]
+1
梅子 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 orchidcat 的回复:]
1、A中是否有记录依照什么判断?
[/Quote]

拿A表中记录
1 aaaa true false 123 2011-02-02 21:33:00

B表中记录:
123 null aaa 2011-01-03 00:00:00
做列子

if(A.session1==true){
if(A.account==B.account){
不插入
}else{
插入
}
}

当然上面这个例子是不完全的,应该比对A表中所有Session1为True的Account是否等于B的Account
然后才能判断是否插入
不知道说明白没。。。
mingpei0703 2011-07-11
  • 打赏
  • 举报
回复
学习下
cd731107 2011-07-11
  • 打赏
  • 举报
回复
--2.
insert a
select * from b where Session2ID is not null
and not exists (select 1 from a where Session2=True and b.Session2=a.Account)
cd731107 2011-07-11
  • 打赏
  • 举报
回复
--1.
insert a
select * from b where Session1ID is not null
and not exists (select 1 from a where Session1=True and b.Session1ID=a.Account)
Mr_Nice 2011-07-11
  • 打赏
  • 举报
回复
1、A中是否有记录依照什么判断?
大力水手 2011-07-11
  • 打赏
  • 举报
回复

insert into table1 select * from table2 where 条件
select * into table2 from table1 where 1<>1 --只建一个相同的表不复制数据
select * into table2 from table1 where 1<>2 --创建表并复制数据...
yubofighting 2011-07-11
  • 打赏
  • 举报
回复

insert into b select * from a where a.account not in(select b.account from b)
快溜 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 fredrickhu 的回复:]
SQL code

if (b.Session1ID is not null)
insert into
a
select
*
from
b where
Session1ID is not null
and
not exists (select 1 from a where Session1=True and b.Session1ID=a.Account)
……
[/Quote].

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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