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

问几个sql语句
1.表A和表B,如何select   AXB?

2.
table   A   (int   KeyA),KeyA是A的key
table   B(int   KeyA,int   value),KeyA是B的key
要求向B插入记录,条件是存在于A但不存在于B的KeyA,value值总是为0




------解决方案--------------------
SELECT *FROM A,B


INSERT INTO B

SELECT KeyA,0 FROM A WHERE NOT EXISTS(SELECT *FROM B WHERE B.KeyA=A.KeyA)
------解决方案--------------------
insert tb select Akey ,0 from ta where Akey not in (select Bkey from tb )
go

------解决方案--------------------
---1、
Select * From A Cross Join B

---2、方法1
Insert Into B(KeyA,Value)
Select KeyA,0 From A Where Not Exists(Select 1 From B Where B.KeyA=A.KeyA)

---2、方法2
Insert B
Select KeyA,0 From A Where KeyA Not In(Select KeyA From B)