请教一个SQL查询语句 急!!!!

z289830131 2010-09-21 01:32:55
有一个表 表名为module
3个字段
moduleCode moduleName
HELP 帮助中心
HELP001 帮助中心
HELP002 帮助中心
HELP003 帮助中心
HELP00101 帮助中心
HELP00102 帮助中心
HELP00103 帮助中心
HELP00104 帮助中心
HELP00105 帮助中心
HELP00106 帮助中心
HELP00107 帮助中心
HELP00108 帮助中心

字段moduleCode不是自动增长列 也不是主键
我现在想写一条查询语句 使得每次都能获得字段moduelCode最后一条数据 比如现在就是查询出HELP00108
请大家帮帮我 不胜感激
...全文
71 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
王向飞 2010-09-21
  • 打赏
  • 举报
回复

--> 数据库版本:
--> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
--> 测试数据:module
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'module')
AND type in (N'U'))
DROP TABLE module
GO

---->建表
create table module([moduleCode] varchar(9),[moduleName] varchar(8))
insert module
select 'HELP','帮助中心' union all
select 'HELP001','帮助中心' union all
select 'HELP002','帮助中心' union all
select 'HELP003','帮助中心' union all
select 'HELP00101','帮助中心' union all
select 'HELP00102','帮助中心' union all
select 'HELP00103','帮助中心' union all
select 'HELP00104','帮助中心' union all
select 'HELP00105','帮助中心' union all
select 'HELP00106','帮助中心' union all
select 'HELP00107','帮助中心' union all
select 'HELP00108','帮助中心'
GO

--> 查询结果
SELECT top 1 *
FROM module order by len(moduleCode) desc,substring(moduleCode,5,len(moduleCode)-4) desc
--> 删除表格
--DROP TABLE module

HSBOY86 2010-09-21
  • 打赏
  • 举报
回复
create table module
(
moduleCode varchar(20),
moduleName varchar(20)
)
insert into module
select 'HELP','帮助中心' union all
select 'HELP001','帮助中心' union all
select 'HELP002','帮助中心' union all
select 'HELP003','帮助中心' union all
select 'HELP00101','帮助中心' union all
select 'HELP00102','帮助中心' union all
select 'HELP00103','帮助中心' union all
select 'HELP00104','帮助中心' union all
select 'HELP00105','帮助中心' union all
select 'HELP00106','帮助中心' union all
select 'HELP00107','帮助中心' union all
select 'HELP00108','帮助中心'

select *,cast(replace(modulecode,'HELP','') as int) as modulecodes from module order by modulecodes desc

select * from module order by cast(replace(modulecode,'HELP','') as int) desc


HSBOY86 2010-09-21
  • 打赏
  • 举报
回复
select *,cast(replace(modulecode,'HELP','') as int) as modulecodes from module order by modulecodes desc


select * from module order by cast(replace(modulecode,'HELP','') as int) desc
z289830131 2010-09-21
  • 打赏
  • 举报
回复
还是不行啊。。。 你写的语句我执行之后 这两个字段的全部数据都查出来了
不过仍然很谢谢你 顶上去啊 千万别沉了
「已注销」 2010-09-21
  • 打赏
  • 举报
回复
try~~
select moduleCode, moduleName
from tb t
where not exists
(select 1 from (select max(right(moduleCode,3)) res from tb
where t.moduleName =moduleName
) m
where right(moduleCode,3)<m.res)
z289830131 2010-09-21
  • 打赏
  • 举报
回复
额。。。。 你这个ID是哪来的呀? 我这张表没有ID 啊
sanny_txx 2010-09-21
  • 打赏
  • 举报
回复
更正下
select max(ID),moduleCode , moduleName   from (select *,row_number() over(order by moduleCode)as ID from module)  
z289830131 2010-09-21
  • 打赏
  • 举报
回复
顶一顶。。。
z289830131 2010-09-21
  • 打赏
  • 举报
回复
谢谢你的回答 但是我执行你写的这条语句会报错呀
错误如下:
开窗函数不能用在其他开窗函数或聚合的上下文中。
sanny_txx 2010-09-21
  • 打赏
  • 举报
回复
select max(row_number() over(order by moduleCode)) from module
z289830131 2010-09-21
  • 打赏
  • 举报
回复
是两个字段 写错了 不好意思

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧