返回列表 发帖

MSSQL的SQL语句(多表交叉查询)

笺注:这是在 使用Navicat远程管理MSSQL2014 的基础上进行的。


使用Navicat的查询编辑器:
图片1.png


在库data1中创建表t_1:
Use data1;

create table t_1 (
id int primary key not null identity (1,1),
name nvarchar(20),
sex nvarchar(2) default '男',
age tinyint default 13,
grade nvarchar(5),address varchar(50),
chinese numeric(12,2),math decimal(12,3));

图片2.png
注释:
字段sex使用了默认值,默认值为 '男'
字段age使用了默认值,默认值为13


表data1.dbo.t_1的表结构:
图片3.png
注释:字段id为主键、标识符列,会自动增长。

图片4.png
注释:
字段sex使用了默认值,默认值为 '男'

图片5.png
注释:
字段age使用了默认值,默认值为13



往表data1.dbo.t_1中一次性插入多条记录:(字段id为主键、标识符列,会自动增长,不用写)

Use data1;

INSERT INTO t_1 (name,sex,age,grade,address,chinese,math) VALUES ('张三','男',13,'三年一班','广州市',108,77);

INSERT INTO t_1 (name,sex,age,grade,address,chinese,math) VALUES ('李小芳','女',14,'三年二班','佛山市',-60,48.5);

INSERT INTO t_1 (name,grade,address,chinese,math) VALUES ('张大白','三年一班','广州市',120,99.5);

注释:第3条记录的字段sex、age使用了默认值,所以这两个字段不用写。



在查询编辑器里输出表data1.dbo.t_1中的所有记录:
Use data1;
SELECT * FROM t_1;
图片6.png





############
############

在旧表的基础上创建新表:
Use data1;
select id,name,grade,chinese into new_t_1 from t_1 where grade = '三年一班';

上面两条代码执行成功后,查看新表data1.dbo.new_t_1的表数据:
SELECT * FROM data1.dbo.new_t_1;
图片7.png



最后重建表data1.dbo.new_t_1的字段“id”:
Use data1;
alter table new_t_1 drop COLUMN id;
alter table new_t_1 add id int primary key not null identity (1,1);

上面两条代码执行成功后,再查看表data1.dbo.new_t_1的表数据:
Use data1;
SELECT * FROM new_t_1;
图片8.png


表data1.dbo.new_t_1的表结构:
图片9.png
注释:字段id为主键、标识符列,会自动增长。














############
############

在库data1中创建表t_2:
Use data1;

create table t_2 (
id int primary key not null identity (1,1),
name nvarchar(20),
phone varchar(30),
english float,
);



往表data1.dbo.t_2中一次性插入多条记录:(字段id为主键、标识符列,会自动增长,不用写)

Use data1;

INSERT INTO t_2 (name,phone,english) VALUES ('张三','11234',60);

INSERT INTO t_2 (name,phone,english) VALUES ('张大白','22234',70.5);



在查询编辑器里输出表data1.dbo.t_2中的所有记录:
Use data1;
SELECT * FROM t_2;
图片10.png





多表交叉查询:(有直接相通的字段时)
Use data1;
select t_1.name,t_1.grade, t_1.chinese,t_2.english from t_1,t_2 where t_1.name = t_2.name;
图片11.png


Use data1;
select t_1.name,t_1.grade, t_1.chinese,t_2.english from t_1,t_2 where t_1.name = t_2.name and t_1.name = '张大白';
图片12.png


Use data1;
select t_1.name,t_1.grade, t_1.chinese,t_2.english from t_1,t_2 where t_1.name = t_2.name and t_1.chinese = 108;
图片13.png


Use data1;
select t_1.name,t_1.grade, t_1.chinese,t_2.english from t_1,t_2 where t_1.name = t_2.name and t_2.english = 70.5;
图片14.png



############

将表data1.dbo.t_1、data1.dbo.t_2的一些字段、记录放到一个新表中:(下面的代码已经包含了创建新表)

Use data1;

select * into new_t_2 from (select t_1.name,t_1.grade, t_1.chinese,t_2.english from t_1,t_2 where t_1.name = t_2.name and t_1.name = '张大白') qqa;


上面两条代码执行成功后,再查看新表data1.dbo.new_t_2的表数据:
Use data1;
SELECT * FROM new_t_2;
图片15.png













##################
##################

在库data1中创建表table1、table2、table3:
Use data1;

create table table1 (id int primary key not null identity (1,1),学生编号 nvarchar(50),姓名 nvarchar(50),住址 nvarchar(50))

create table table2 (id int primary key not null identity (1,1),学生编号 nvarchar(50),姓名 nvarchar(50),自选课程名称 nvarchar(20))

create table table3 (id int primary key not null identity (1,1),课程名称 nvarchar(20),课程老师 nvarchar(20))



插入记录:(字段id为主键、标识符列,会自动增长,不用写)
Use data1;

insert into table1 values ('A001','小明','佛山')

insert into table1 values ('A002','小李','广州')

insert into table1 values ('A003','小海','肇庆')


insert into table2 values ('A001','小明','跆拳道')

insert into table2 values ('A002','小李','空手道')

insert into table2 values ('A003','小海','跆拳道')


insert into table3 values ('跆拳道','李大杰')

insert into table3 values ('空手道','小龙')



在查询编辑器里一次性输出表data1.dbo.table1、data1.dbo.table2、data1.dbo.table3中的所有记录:

Use data1;

Select * from table1;
Select * from table2;
Select * from table3;

图片16.png

图片17.png

图片18.png



多表交叉查询:(没有直接相通的字段时)

查询选择了课程“空手道”的学生信息,并显示对应的课程老师:
Use data1;
select table1.学生编号,table1.姓名, table1.住址,table2.自选课程名称,table3.课程老师 from table1,table2,table3 where table1.学生编号 = table2.学生编号 and table2.自选课程名称 = table3.课程名称 and table3.课程名称 = '空手道';
图片19.png
笺注:要有间接相通的字段,才能进行查询。



查询选择了课程“跆拳道”的学生信息,并显示对应的课程老师:
Use data1;
select table1.学生编号,table1.姓名, table1.住址,table3.课程名称,table3.课程老师 from table1,table2,table3 where table1.学生编号 = table2.学生编号 and table2.自选课程名称 = table3.课程名称 and table3.课程名称 = '跆拳道';
图片20.png



############

将表data1.dbo.table1、data1.dbo.table2、data1.dbo.table3的一些字段、记录放到一个新表中:(下面的代码已经包含了创建新表)

Use data1;

select * into new_table4 from (select table1.学生编号,table1.姓名, table1.住址,table3.课程名称,table3.课程老师 from table1,table2,table3 where table1.学生编号 = table2.学生编号 and table2.自选课程名称 = table3.课程名称 and table3.课程名称 = '跆拳道') qqa;


上面两条代码执行成功后,再查看新表data1.dbo.new_table4的表数据:
Use data1;
SELECT * FROM new_table4;
图片21.png





相关文章:
MSSQL的SQL语句
Windows2012R2_安装MySQL5.5

返回列表