加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Insert into 多条数据记录以及集合的SQL语句

(2013-05-16 10:51:03)
分类: SQL

(一) insert into多条记录--数据项

在mssql可以这样
insert into tablefortest(a,b) 
select 1,2
union
select 3,4
union
select 5,6

在mysql可以这样

insert into tablefortest(a,b)values(1,2) ,(3,4) ,(5,6)

insert into base_datadictionary(dataItemNum,dataItemName,fatherItemId,dataItem,isLeaf,remark) values(2,'973、863项目',2917,'',1,''),
(4,'国家社科规划、基金项目',2917,'',1,''),
(5,'教育部人文、社会科学研究项目',2917,'',1,''),
(6,'国家自然科学基金项目',2917,'',1,''),
(7,'中央、国家各部门项目',2917,'',1,''),
(9,'省(自治区、直辖市)项目',2917,'',1,''),
(12,'国际合作研究项目',2917,'',1,''),
(13,'与港、澳、台合作研究项',2917,'',1,''),
(14,'企、事业单位委托项目',2917,'',1,''),
(15,'外资项目',2917,'',1,''),
(16,'学校自选项目',2917,'',1,''),
(17,'国防项目',2917,'',1,''),
(90,'非立项',2917,'',1,''),
(99,'其他',2917,'',1,'');

(二) insert into select多条记录---数据集

用法:  INSERT INTO t2(t2.f1,t2.f2, http://www.cnblogs.com/Images/dot.gifinto 多条数据记录以及集合的SQL语句" />)FROM t1

语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1 

--1.创建测试表
    create TABLE Table1
    (
        
varchar(10),
        
varchar(10),
        
varchar(10),
        
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
        (
            
ASC
        )
    
ON [PRIMARY]

    
create TABLE Table2
    (
        
varchar(10),
        
varchar(10),
        
int,
        
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
        (
            
ASC
        )
    
ON [PRIMARY]
    
GO
    
--2.创建测试数据
    Insert into Table1 values('','asds','90')
    
Insert into Table1 values('','asds','100')
    
Insert into Table1 values('','asds','80')
    
Insert into Table1 values('','asds',null)
    
GO
    
select * from Table2

    
--3.INSERT INTO SELECT语句复制表数据
    Insert into Table2(a, c, d) select a,c,5 from Table1       //我们除了插入源表Table1的字段外,还可以插入常量
    
GO

    
--4.显示更新后的结果
    select * from Table2
    
GO
    
--5.删除测试表
    drop TABLE Table1
    
drop TABLE Table2

(三) SQL集合运算-- 差集、交集、并集

-------------------------------------

--  SQL集合运算——查询的差集      --

-------------------------------------

--求没有选择课号为‘c01’的学生学号

 

 

 

select sid from learning

  where cid<>'c01'

 

 

 

select sid from learning

  except (

    select sid from learning where cid='c01'

  )

 

--求没有选择课号为‘c01’的学生的学号和课号

 

 

 

select sid,cid from learning

  except (

    select sid,cid from learning where cid='c01'

  )

 

--正确:

 

select sid,cid from learning

  where sid in (

    select sid from learning

      except (

        select sid from learning where cid='c01'

      )

  )

 

-------------------------------------

--  SQL集合运算——查询的交集      --

-------------------------------------

--求同时选了课号为'c01'和'c02'的学生学号

 

 

 

select sid from learning where cid='c01' and cid='c02'

 

 

 

select sid from learning where cid='c01'

  intersect (

    select sid from learning where cid='c02'

  )

 

 

 

select l1.sid from learning l1,learning l2

  where l1.sid=l2.sid and l1.cid='c01' and l2.cid='c02'

 

--问题:求同时选了课号为'c01'和‘c02’的学生学号和课号。

 

-------------------------------------

--  SQL集合运算——查询的并集      --

-------------------------------------

--求计算机系学生的学号和选了'c03'课程且成绩在分以上的学生学号

 

select sid from student where department='计算机系'

  union (

    select sid from learning

      where cid='c03' and score>80

  )

 

--分解分析,两个子查询自动将重复出现的记录合并

 

select sid from student where department='计算机系'

select sid from learning

      where cid='c03' and score>70

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有