JWH内容
A1-1
A1-2
A2-1
A2-4
A3-1
A20-1
A9-1
A9-3
B1-2
B3-2
B10-2
B9-3
等等
为什么 ORDER BY JWH 后,结果是下面的,注意9的排序
A1-1
A1-2
A20-1
A2-1
A2-4
A3-1
A9-1
A9-3
B1-2
B10-2
B3-1
B9-3
似乎只排序到A1这两位数字上。A10就在A2前面,9反而成了最后了
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-07-16 11:39:32
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([aaa] varchar(5))
insert [tb]
select 'A1-1' union all
select 'A1-2' union all
select 'A2-1' union all
select 'A2-4' union all
select 'A3-1' union all
select 'A20-1' union all
select 'A9-1' union all
select 'A9-3' union all
select 'B1-2' union all
select 'B3-2' union all
select 'B10-2' union all
select 'B9-3'
--------------开始查询--------------------------
select * from [tb] order by charindex('-',aaa)
----------------结果----------------------------
/*aaa
-----
A1-1
A1-2
A2-1
A2-4
A3-1
A9-1
A9-3
B1-2
B3-2
B9-3
B10-2
A20-1
(所影响的行数为 12 行)
*/
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([aaa] varchar(5))
insert [tb]
select 'A1-1' union all
select 'A1-2' union all
select 'A2-1' union all
select 'A2-4' union all
select 'A3-1' union all
select 'A20-1' union all
select 'A9-1' union all
select 'A9-3' union all
select 'B1-2' union all
select 'B3-2' union all
select 'B10-2' union all
select 'B9-3'
select * from tb order by substring(aaa,1,1) asc, charindex('-',aaa) asc
/*
A1-1
A1-2
A2-1
A2-4
A3-1
A9-1
A9-3
A20-1
B1-2
B3-2
B9-3
B10-2
*/
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================
if object_id('[tb]') is not null
drop table [tb]
go
create table [tb]([aaa] varchar(5))
insert [tb]
select 'A1-1' union all
select 'A1-2' union all
select 'A2-1' union all
select 'A2-4' union all
select 'A3-1' union all
select 'A9-1' union all
select 'A8-1' union all
select 'A9-3' union all
select 'B1-2' union all
select 'B3-2' union all
select 'B7-2' union all
select 'B9-3'
go
select aaa from tb
order by left(aaa,1),RIGHT(aaa,LEN(aaa)-CHARINDEX('-',aaa)),substring(aaa,2,CHARINDEX('-',aaa)-2)
/*------------
(12 行受影响)
aaa
-----
A1-1
A2-1
A3-1
A8-1
A9-1
A1-2
A9-3
A2-4
B1-2
B3-2
B7-2
B9-3
-------*/
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================
if object_id('[tb]') is not null
drop table [tb]
go
create table [tb]([aaa] varchar(5))
insert [tb]
select 'A1-1' union all
select 'A1-2' union all
select 'A2-1' union all
select 'A2-4' union all
select 'A3-1' union all
select 'A9-1' union all
select 'A20-1' union all
select 'A9-3' union all
select 'B1-2' union all
select 'B3-2' union all
select 'B10-2' union all
select 'B9-3'
go
select aaa from tb
order by left(aaa,1),RIGHT(aaa,LEN(aaa)-CHARINDEX('-',aaa)),
case when substring(aaa,2,CHARINDEX('-',aaa)-2)<10 then '0'+substring(aaa,2,CHARINDEX('-',aaa)-2) else substring(aaa,2,CHARINDEX('-',aaa)-2) end
/*------------
(
(12 行受影响)
aaa
-----
A1-1
A2-1
A3-1
A9-1
A20-1
A1-2
A9-3
A2-4
B1-2
B3-2
B10-2
B9-3
(12 行受影响)
-------*/
if object_id('[tb]') is not null drop table [tb]
create table [tb]([aaa] varchar(5))
insert [tb]
select 'A1-1' union all
select 'A1-2' union all
select 'A2-1' union all
select 'A2-4' union all
select 'A3-1' union all
select 'A20-1' union all
select 'A9-1' union all
select 'A9-3' union all
select 'B1-2' union all
select 'B3-2' union all
select 'B10-2' union all
select 'B9-3'
select * from tb order by substring(aaa,1,1),cast(substring(aaa,2,charindex('-',aaa)-2) as int)
----
(12 行受影响)
aaa
-----
A1-1
A1-2
A2-1
A2-4
A3-1
A9-1
A9-3
A20-1
B1-2
B3-2
B9-3
B10-2
(12 行受影响)
-----
if object_id('[tb]') is not null drop table [tb]
create table [tb]([aaa] char(10))
insert [tb]
select 'A1-1' union all
select 'A1-2' union all
select 'A2-1' union all
select 'A9-1' union all
select 'A9-3' union all
select 'A2-4' union all
select 'A3-1' union all
select 'A20-1' union all
select 'B1-2' union all
select 'B3-2' union all
select 'B10-21' union all
select 'B10-9' union all
select 'B9-3'
SELECT *
FROM [tb]
ORDER BY LEFT(aaa,1)
,CAST(SUBSTRING(aaa,2,CHARINDEX('-',aaa) - 2 ) AS INT)
,CAST(SUBSTRING(aaa,CHARINDEX('-',aaa) + 1,LEN(aaa) - CHARINDEX('-',aaa)) AS INT)
第二个排序为什么会按照s_id 进行排序,该怎么理解这两个代码 select a.s_id,a.s_score ,a.c_id, @i:=@i+1 as rank from score a,student b ,(select @i:=0)s where a.c_id='01' and a.s_id=b.s_id order by a.s_...
先了解下sql的执行顺序 select [column1],[column2] from [Table] where [column1]='1' group by [column1],[column2] order by [column2] 执行顺序: 1:where 2:group by (having ) 3:order by 问题:同一...
事情是这样的,有个问题,先看如下表Department: 部门ID 部门 1 总经理 2 人事部 3 市场部 4 销售部 5 技术部 现在问题来了,想显示时,把技术部和总经理放在最上面,然后再把其它的按部门升序排列,怎么解决? ...
我在视图中 用SQL定义了我的表 执行之后 下面显示的排序是正确的 可是 我点击视图的显示结果的时候 并没有按我定义的(即order by 赞助商名字)排序 为什么呢? ...
Sql排名问题 查询业绩前三排名
sql面试题:topN问题 4.多表查询 【面试题类型总结】这类题目属于行列如何互换,解题思路如下: 其他面试题: SQL基础知识整理: select 查询结果 如:[学号,平均成绩:组函数avg(成绩)]from 从哪张表中查找数....
1.对指定单列进行排序 查询分数表中编号为1的所有分数信息,并用下列两种方式排序 1.1升序排序 SELECT * FROM scores WHERE gno=1 ORDER BY score ASC 1.2降序排序 SELECT * FROM scores WHERE gno=1 ORDER...
ORDER BY 语句用于对结果集进行排序。 ORDER BY 语句用于根据指定的列对结果集进行排序。 ORDER BY 语句默认按照升序对记录进行排序。 如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。 原始的表 (用...
SQL排序子句 ORDER BYORDER BY : 排序语句,默认升序,DESC降序!
比如需要对SQL表中的字段NAME进行如下的排序: 张三(Z) 李四(L) 王五(W) 赵六(Z) 按照sql中的默认排序规则,根据字母顺序(a~z)排,结果为:李四 王五 赵六 张三 自定义排序:order by charindex(NAME,...
我有一个小问题,想了半天,请大侠出手帮助一下,谢谢。 我能用笨的方法解决,但是不知道简单的方法。见例子: T1(序号,名称,数量) 1,A,100 2,B,50 3,C,10 T2(名称,每箱) A,30 B,25 C,20 序号输出...
Order by排序查询 升序排列asc 降序排列desc 汉字笔画排序 音序排序(abc) 多列排序 随机排序 显示部分记录的排序top 区别: 计算字段排序 按需求动态排序 case Order by排序查询 升序排列asc ...
排序(升序,降序) select 语句 from 表名 order by //order by是最后执行的 1.按照工资升序,找出员工名和薪资? select ename,sal from emp order by sal; //排序默认的是升序 +--------+-...
SQL岗位30个面试题,SQL面试问题及答案: 什么是SQL? SQL(结构化查询语言)是一种设计用于检索和操作数据的数据库。它属于美国国家标准协会(ANSI)的一种标准,可用于执行Select(选择)、Update(更新)、...
需求描述 查询表中名字带指定关键字的数据 完全匹配放在第一位 前匹配放在第二位 末尾匹配放在第三位 中间匹配放在第四位 建表DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` varchar(20) NOT NULL,...
创建一张学生表(姓名,年龄,零花钱),插入六条数据CREATE TABLE student ( name VARCHAR2(20), age NUMBER(5), money NUMBER(5) ) INSERT INTO student VALUES('张1',7,50); INSERT INTO student VALUES('张2',7...
代码: 案例一: SELECT* FROMt_example_library_node WHERE name='dede' ORDER BY STATE , NAME 案例二: SELECT * from t_example_library_node order by STATE asc... ...
SELECT * FROM ( (SELECT rp1.id FROM rb_problem rp1 WHERE rp...这两个sql分开查询时sql1的结果为32,30,sql2的结果为31,但是当用union all拼接起来时为何结果就变成30,32,31了,我的子查询语句的排序完全没有作用了
create view studentScore (studentNo,studentname,courseName,score)as select S.studentNo,studentname,courseName,score from Student S join Score on S.studentNo=Score.studentNo join Course on Cours.....
对每组的数据按日期正序排序并加上行号,取出时只取行号为1,也就是第一条数据。 1、row_number() over()排序功能: (1) row_number() over()分组(无重复)排序功能: 在使用 row_number() over()函数...
运行sql语句: select * from test order by name,time desc 结果: 备注:可以在查询基础上再用where语句查询 例如:select top 1 * from (select * from test order by name,time desc) where na...
时间字段1 时间字段2,这两个字段只有一个有时间,时间字段1有数据,时间字段2就没有,反之亦然,如何实现按时间大小排序(字段1有时间就按字段1,2也是)
在Oracle中进行查询排序时,如果排序字段里面有空值的情况下,排序结果可能会达不到自己想要的结果。 如 select * from tableTest order by VISITS desc 将原来的sql语句改写为: select * from tableTest ...
当我们排序想把空值放到最后的时候,怎么写呢?不多BB,直接上SQL。 正序: ORDER BY 字段名 NULLS LAST 倒序: ORDER BY 字段名 DESC NULLS LAST
1.升序排序 ASC 默认排序规则 2.降序排序 DESC select * from XXX order by XXX.xxx desc 3.自定义排序 (1).CHARINDEX 通过CHARINDEX如果能够找到对应的字符串,则返回该字符串位置,否则返回0。 基本语法...
最后在做一个 教育系统,班级排序遇到一个问题,10班 11班 老是排在 1 班前面,测试的妹子要求 10班11班 要排在9班后面。最后找到一 个解决方法了:利用oracle函数及正则表达式进行排序order by to_number(regexp_...
1,sqlserver中给top动态传值。 DECLARE @topn AS int" SET @topn=?" select Top (@topn) * from user_info where order by green_beans desc,nativeQuery=true)sql server:读取前10条:select top (10)...
sql 排序问题: 1.假设排序时我们用时间最为排序条件,那么在倒序时会出现空值排在前面,有值的排在后面。 解决:将排序字段加 is null; 例1:空值排在前面 select * from sys_user order by update_date desc...
!... Oracle数据库,Version 值全部为大写英文字母序列,A .B. C ...Z 然后AB. AC....... 如图:如何返回Version最大的哪一组,即返回 Version为DC的哪两行 ...有个思路,能否对Version列按字符拆分,然后分别取ASCII在求和...
使用Java提供的 Collator 类来进行中文字符串的...优点:排序方法实现简单. 缺点:排序结果不是很准确. 例子: String[] titles = {"关羽","张飞","公孙瓒","诸葛亮","曹操","刘备","赵云","微微", "哈哈", "哈"...