怎么用最简单的方法判断行的数据是从小到大排列的?
时间:2011-12-04
来源:互联网
create table #t(age int) insert #t select 25 union select 35 union select 30 select * from #t age -------------------- 25 30 35 (3 行受影响)
测试数据如上,25,30,35是从小到大排列,要打印出“合法”
当数据为25,35,30是因为第二列35>第三列30,要打印出“不合法”
用SQL如何实现?
(备注:行数不固定)
作者: gdjlc 发布时间: 2011-12-04
你只列出了一个列,又要不按某种排序方式来确定它是否排列合法,本身就是不通的逻辑.
这个表的其他列是什么样的呢?
作者: qianjin036a 发布时间: 2011-12-04
作者: gdjlc 发布时间: 2011-12-04
作者: gdjlc 发布时间: 2011-12-04
SQL code
create table #t(age int) insert #t select 35 union select 25 union select 30 select * from #t --or create table #t(age int) insert #t select 30 union select 35 union select 25 select * from #t --or create table #t(age int) insert #t select 25 union select 35 union select 30 select * from #t --上面的语句不管怎么排法,所得到的结果都只有一个: /* age ----------- 25 30 35 (3 行受影响) */
你可以自己测试,反正我测得的结果就是这样.
由此可知,不按某种方式进行排序,就对行的排列确定先后顺序,是不靠谱的.
作者: qianjin036a 发布时间: 2011-12-04
用union all select create table #t(age int) insert #t select 25 union all select 35 union all select 30 select * from #t /* 25 35 30 */
作者: roy_88 发布时间: 2011-12-04
作者: qianjin036a 发布时间: 2011-12-04
SQL code
用union all select
create table #t(age int)
insert #t select 25
union all select 35
union all select 30
select * from #t
/*
25
35
30
*/
不好意思,我在一楼写错了,就是这种格式
作者: gdjlc 发布时间: 2011-12-04
你那个文本文件是如何读到数据表里来的呢?
是用xp_cmdshell
作者: gdjlc 发布时间: 2011-12-04
引用 6 楼 qianjin036a 的回复:
你那个文本文件是如何读到数据表里来的呢?
是用xp_cmdshell
你的表,最好有一个自增列,这样,数据一行行从txt文件读来插入的时候,就有一个插入顺序,你可以根据这个插入顺序来判断读来的数据是否按升序或降序排列,判断语句如:
SQL code
create table #t(id int identity(1,1),age int) insert #t select 25 union all select 35 union all select 30 go select case when exists(select 1 from #t a where exists(select 1 from #t where id>a.id and age<a.age)) then '不合法' else '合法' end /* ------ 不合法 (1 行受影响) */ go drop table #t go create table #t(id int identity(1,1),age int) insert #t select 25 union all select 30 union all select 35 go select case when exists(select 1 from #t a where exists(select 1 from #t where id>a.id and age<a.age)) then '不合法' else '合法' end /* ------ 合法 (1 行受影响) */ go drop table #t
作者: qianjin036a 发布时间: 2011-12-04
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28