你看我的了么?我的不可能错,至少我的思路是完全完全正确的
你再仔细看看,这是我测试过的,你好好看看
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[news]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[news]
GO
CREATE TABLE [dbo].[news] (
[newsid] [int] IDENTITY (1, 1) NOT NULL ,
[newstitle] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[newstype] [int] NULL
) ON [PRIMARY]
GO
select *,0 as sortid
from news a
where a.newsid = (select max(newsid) from news c where c.newstype = '1')
union all
select *,newsid as sortid from news b where b.newstype = 0
order by sortid
SELECT A.newsid, A.newstitle, A.newstype,
CASE WHEN newsid = (SELECT MAX(newsid) AS newsid FROM news WHERE newstype = 1)
THEN -1
ELSE newsid
END AS NUN
FROM news A
ORDER BY 4
抛砖引玉吧,我写了个最土的方法:
select * from news where newsid=(select max(newsid)from news)union all select * from news where newsid<>(select max(newsid)from news)
希望师兄们写出更好的,请赐教谢谢。
select a.newstitle as title,0 as sortid from news a where a.id = (select max(id) from news c where c.type = '1')
union all
select b.newstitle as title,id as sortid from news b where b.newstype = 0
order by sortid
大致就是这个意思,差不多吧,我没试。
你的ID应该是从1开始增长的吧