select * from Student as a,subject as b where a.id=b.id
对应关系
一个Student的id对应多个subject的id
联合查询会造成显示全部的课程列表
我是想怎么查询出来的结果是 每个学生一条记录而课程只显示对应的第一条记录就可以了不用显示全部的
如何查询呢
表结构Student表的结构字段 名字
Id 编号
Name 名字 subject的结构
字段 名字
Id 编号
SID 对应Student表的编号
Name 名字
对应关系
一个Student的id对应多个subject的id
联合查询会造成显示全部的课程列表
我是想怎么查询出来的结果是 每个学生一条记录而课程只显示对应的第一条记录就可以了不用显示全部的
如何查询呢
表结构Student表的结构字段 名字
Id 编号
Name 名字 subject的结构
字段 名字
Id 编号
SID 对应Student表的编号
Name 名字
from Student a,
(select SID ,max(Name) as name from subject group by sid ) b
where a.id=b.SID
(
select t.* , row_number() over(partition by a_id ) rn
(select a.id a_id ,b.id b_id,a.name a_name,b.name b_name ,sid from Student a,
subject b
where a.id=b.id) t)
where rn = '1'
from (select a.*,
b.*,
row_number() over(partition by a.id order by a.id) rn
from student a,subject b
where a.id = b.SID)
where rn = 1
select * from Student as a,
(select * from subject where id in(select max(id) from subject group by sid)) b
where a.id=b.sid--分析函数
select t.id,t.sid,t.name,a.id,a.name
(select id,sid,name,row_number() over(partition by sid order by id desc) rn
from subject) t,Student a
where a.id=t.sid and rn=1
这是去除单表重复值的SQL Delete from b_table b where b.rowid >
(select min(s.rowid) from s_table s where b.id = s.id--此条件还可以是其他两记录相同的值)