您的位置:程序门 -> ms-sql server -> 基础类



问几个sql语句


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


问几个sql语句[已结贴,结贴人:jadetiger]
发表于:2007-09-09 00:32:21 楼主
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


发表于:2007-09-09 17:14:321楼 得分:20
---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   keya=a.keya)

---2、方法2
insert   b
              select   keya,0   from   a   where   keya   not   in(select   keya   from   b)
发表于:2007-09-09 18:43:172楼 得分:0
测试了一下楼上的

1、
select   *   from   a   cross   join   b

2、
insert   into   b(keya,value)
(select   keya, '0 '   from   a   where   keya   not   in(select   keya   from   b))

这样可以!
发表于:2007-09-10 00:23:523楼 得分:0
1.表a和表b,如何select   axb?
select   a.*   ,   b.*   from   a   cross   join   b   where   a.id   =   b.id

2.
table   a   (int   keya),keya是a的key
table   b(int   keya,int   value),keya是b的key
要求向b插入记录,条件是存在于a但不存在于b的keya,value值总是为0
insert   into   b   select   keya   ,   value   =   0   from   a   where   keya   not   in   (select   keya   from   b)


发表于:2007-09-10 08:11:394楼 得分:0
我觉得插入难度大了点,直接做一个左连接插入到新表行不行?
比如这样:select   ……insert   newtable   form   table1   left   join   table2   on     table1。field=table2.field
发表于:2007-09-10 08:38:145楼 得分:0
1.表a和表b,如何select   axb?
select   a.*   ,   b.*   from   a   cross   join   b  
2.
table   a   (int   keya),keya是a的key
table   b(int   keya,int   value),keya是b的key
要求向b插入记录,条件是存在于a但不存在于b的keya,value值总是为0
insert   into   b   select   keya   ,   value   =   0   from   a   where   keya   not   in   (select   keya   from   b)



快速检索

最新资讯
热门点击