表中结构如下id nameA1 aaaA2 bbbA3 cccA4 dddA5 eee
我现在想查出来最大的id 应该怎么写?
我现在想查出来最大的id 应该怎么写?
解决方案 »
- 求救 文本+数字 的 varchar转换成数字型。。
- dbcc权限
- 可以关闭日记记录功能吗?
- SQL数据库特别问题
- UP者有分:数据库学习,路在何方??同时祝大家新年快乐!!
- 如何通过sql命令得到触发器的内容?
- 怎样做一个通用程序,把一个表的数据移到另外一个表
- 我的数据库服务器为SQL SERVER 7.0+SP2/(NT SERVER 4.0+SP6),我现在想在该台服务器上增加一个CPU,请问SQL SERVER 会不会自动识别,会不会造成SQL SERVER的不稳定?
- 插入数据的问题
- 巨难的问题.....相信大家都可能会遇到, 将打竖的数据变成打横的数据的问题
- 装了SQL2005,安装成功后出现两个服务器引擎
- sql如何判断是否有符合条件的记录?
select top 1 * from tb order by id desc
--楼上的是按字符串来排序的,--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id varchar(20),name varchar(3))
insert into [tb]
select 'A1','aaa' union all
select 'A2','bbb' union all
select 'A3','ccc' union all
select 'A4','ddd' union all
select 'A5','eee' union all
select 'A11','eee'select top 1 id from [tb] order by cast(substring(id,patindex('%[0-9]%',id),len(id)) as int) desc--结果:
A11
FROM dbo.Student
ORDER BY SName DESC
union all select 'A2','bbb'
union all select 'A3','ccc'
union all select 'A4','ddd'
union all select 'A5','eee'select MAX(id) max_id from #tb
/*
max_id
------
A5(1 row(s) affected)
*/
8 a1 aaa 0 男 班长 aa aaa 啊啊啊啊啊
9 a2 aaa 0 男 班长 aa aaa 啊啊啊啊啊
10 a3 aaa 0 男 班长 aa aaa 啊啊啊啊啊
11 b1 NULL NULL NULL NULL NULL NULL NULL
12 b2 NULL NULL NULL NULL NULL NULL NULL
13 b3 NULL NULL NULL NULL NULL NULL NULLSELECT TOP 1 *
FROM dbo.Student
ORDER BY SName DESC
13 b3 NULL NULL NULL NULL NULL NULL NULL
(id,patindex('%[0-9]%',id),len(id)) as int
然后使用substring进行截取,相当于在id字段中,截取字段里面对应的数字进行排序。。