34,590
社区成员
发帖
与我相关
我的任务
分享
select 学生表.*,班级类别.分科
from 学生表
join 班级类别
on 学生表.班级编号=班级类别.编号
或者是
select 学生表.*,班级类别.分科
from 学生表,班级类别
where 学生表.班级编号=班级类别.编号
select
xs.*,
bj.分科
from
学生表 xs join 班级类别 bj
on xs.班级编号=bj.编号
select
a.*,
b.分科
from
学生表 a
left join
班级类别 b
on
a.班级编号=b.编号
if object_id('[学生表]') is not null drop table [学生表]
go
create table [学生表]([编号] int,[姓名] varchar(10),[班级编号] int)
insert [学生表] select 1,'张三',1
union all select 2,'李四',1
union all select 3,'王五',2
if object_id('[班级类别]') is not null drop table [班级类别]
go
create table [班级类别]([编号] int,[名称] varchar(10),[分科] varchar(10))
insert [班级类别] select 1,'高三3班','文'
union all select 2,'高二1班','理'
union all select 3,'高二2班','理'
select X.*,B.[分科] from [学生表] X join [班级类别] B on X.[班级编号]=B.[编号]
编号 姓名 班级编号 分科
----------- ---------- ----------- ----------
1 张三 1 文
2 李四 1 文
3 王五 2 理
(3 行受影响)
if object_id('[学生表]') is not null drop table [学生表]
go
create table [学生表]([编号] int,[姓名] varchar(10),[班级编号] int)
insert [学生表] select 1,'张三',1
union all select 2,'李四',1
union all select 3,'王五',2
if object_id('[班级类别]') is not null drop table [班级类别]
go
create table [班级类别]([编号] int,[名称] varchar(10),[分科] varchar(10))
insert [班级类别] select 1,'高三3班','文'
union all select 2,'高二1班','理'
union all select 3,'高二2班','理'
select X.*,B.[分科] from [学生表] X join [班级类别] B on X.[班级编号]=B.[编号]
if object_id('[学生表]') is not null drop table [学生表]
go
create table [学生表]([编号] int,[姓名] varchar(10),[班级编号] int)
insert [学生表] select 1,'张三',1
union all select 2,'李四',1
union all select 3,'王五',2
if object_id('[班级类别]') is not null drop table [班级类别]
go
create table [班级类别]([编号] int,[名称] varchar(10),[分科] varchar(10))
insert [班级类别] select 1,'高三3班','文'
union all select 2,'高二1班','理'
union all select 3,'高二2班','理'
select a.*,b.分科
from 学生表 a
join 班级类别 b
on a.班级编号=b.编号
/*
编号 姓名 班级编号 分科
----------- ---------- ----------- ----------
1 张三 1 文
2 李四 1 文
3 王五 2 理
(3 行受影响)
*/
--另一种方法:
select *,分科=(select 分科 from 班级类别 where 编号=t.班级编号) from [学生表] t
/*
编号 姓名 班级编号 分科
----------- ---------- ----------- ----------
1 张三 1 文
2 李四 1 文
3 王五 2 理
(3 行受影响)
*/
select a.*,b.分科
from 学生表 a
,班级类别 b
where a.班级编号=b.编号
select a.*,b.分科
from 学生表 a
,班级类别 b
where a.班级编号=b.编号
select a.*,b.分科
from 学生表 a
join 班级类别 b
on a.班级编号=b.编号