34,593
社区成员
发帖
与我相关
我的任务
分享
---建立测试数据create table tfile(fileid int,filename varchar(20),filepath varchar(30),subjectid int)
insert into tfile
select 1 ,'file1', 'img/file1.jpg', 1 union all
select 2 ,'file2', 'img/file2.jpg', 1 union all
select 3 ,'file3', 'img/file3.jpg', 1
create table tsubject(subjectid int ,subjectname varchar(20))
insert into tsubject
select 1 ,'photo' union all
select 2 ,'life'
select b.fileid,b.filename,b.filepath,
a.subjectid,a.subjectname
from tsubject as a
left join
(
select fileid,filename,filepath,subjectid from tfile as c where fileid=(select max(fileid) from tfile where subjectid=c.subjectid)
)as b on a.subjectid=b.subjectid
--
3 file3 img/file3.jpg 1 photo
NULL NULL NULL 2 life
select * from 表名 我要带颜色显示
select b.fileid,
b.filename,
b.filepath,
a.*
from (select *
from tsubject t
where not exists(select 1 from tsubject where subjectid = t.subjectid and fileid> t.fileid)) a
left join tfile b on a.subjectid = b.subjectid
create table tfile(fileid int,[filename] varchar(10),filepath varchar(20),subjectid int)
create table tsubject(subjectid int ,subjectname varchar(10))
insert into tfile values(1,'file1','img/file1.jpg',1)
insert into tfile values(2,'file2','img/file2.jpg',1)
insert into tfile values(3,'file3','img/file3.jpg',1)
insert into tsubject values(1,'photo')
insert into tsubject values(2,'life')
select fileid,filepath,[filename],c.subjectid,subjectname from tsubject c left join
(select * from tfile a where not exists(select 1 from tfile where fileid>a.fileid))d
on c.subjectid=d.subjectid
fileid filepath filename subjectid subjectname
----------- -------------------- ---------- ----------- -----------
3 img/file3.jpg file3 1 photo
NULL NULL NULL 2 life
(2 行受影响)
select b.fileid,
b.filename,
b.filepath,
a.*
from select *
from tsubject t
where not exists(select 1 from tsubject where subjectid = t.subjectid and fileid> t.fileid) a
left join tfile b on a.subjectid = b.subjectid
select c.fileid,c.filename,c.filepath,b.* from
(select * from tfile a where not exists(select 1 from tfile where subjectid=a.subjectid and fileid>a.fileid))c right join tsubject b
on c.subjectid = b.subjectid
我要实现这种效果
fileid filename filepath subjectid subjectname
3 file3 img/file3.jpg 1 photo
null null null 2 life
---
select b.fileid,
b.filename,
b.filepath,
a.*
from tsubject a
left join tfile b on a.subjectid = b.subjectid