日期:2014-05-18  浏览次数:20589 次

把tb2中的marketId ,StockId不存在于tb1中的记录插入到tb1中。这样写么???
insert into tb1 (MarketId, StockId, NewPrice) 
select MarketId, StockId, NewPrice 
from tb2
where not ( MarketId in (select MarketId from tb1) and StockId in (Select StockId from tb1))

把tb2中的marketId ,StockId不存在于tb1中的记录插入到tb1中。这样写么???

另外 下面这句和上面这句一样么??转不过弯来了。感觉不一样啊。。。

insert into tb1 (MarketId, StockId, NewPrice) 
select MarketId, StockId, NewPrice 
from tb2
where ( MarketId not in (select MarketId from tb1) and StockId not in (Select StockId from tb1))

------解决方案--------------------
SQL code

insert tb1
select MarketId, StockId, NewPrice from tb2 a
where not exists(select 1 from tb1 a where a.MarketId=b.MarketId and a.StockId=b.StockId)

------解决方案--------------------
SQL code

insert into tb1 (MarketId, StockId, NewPrice)  
select MarketId, StockId, NewPrice  
from tb2
where not ( MarketId in (select MarketId from tb1) and StockId in (Select StockId from tb1))
--这句错了
insert into tb1 (MarketId, StockId, NewPrice)  
select MarketId, StockId, NewPrice  
from tb2
where MarketId not in(select MarketId from tb2)
and StockId not in(select StockId  from tb2)

--这样写可以,但是逻辑上有问题,还是用exists的吧