A表只有一列id,数据值连续的从1到100,第一个语句能查询到前10条记录,第二个语句本想查到id号为10的记录,执行后却显示了id为100的记录select * from (select top 10 * from A) as t
select max(id) from (select top 10 * from A) as t 我认为第二条语句首先获得了前10条记录作为结果集,然后再从这个结果集中筛选出最大id号的记录.可是执行后结果却不是的,我想请教第二条语句为什么会是这样的结果,分析这条语句的工作机制?
select max(id) from (select top 10 * from A) as t 我认为第二条语句首先获得了前10条记录作为结果集,然后再从这个结果集中筛选出最大id号的记录.可是执行后结果却不是的,我想请教第二条语句为什么会是这样的结果,分析这条语句的工作机制?
解决方案 »
- 数据库安全应用 使用MySQL的23个注意事项
- 一个三表联接后分类汇总的SQL语句
- 急需DELHPI编程熟手,找工作的请进!!!
- 求sql的解决方案跟见解 在线等各位回答。
- 请问各位老师,SqlServer2000的视图中怎么在where条件中使用随机数阿,能否用随机函数阿,是什么随机函数阿。请赐教。
- 如何去掉纪录中的空格
- 求取出每年最新的数据的sql
- 数据库出错,邹建在那里?
- 关于SQL语名的问题
- 在查询时 如何把一个时间字段的查询结果格式化为如:“2015-03-14 08:05:02”;而不是默认的“2015-03-14 08:05:02” ;相关的s
- 一个看似简单的sql,告急!
- 关于触发器的执行
Try
select max(id) from (select top 10 * from A order by id) as t
--这样写
select max(id) from (select top 10 * from A order by ...) as t --用max的时候好像先默认对id排序了,然后执行里面的select top,
--其实这个时候的结果跟 单独执行select top语句的结果不一样了
我觉的4楼的解释可能是对的.
还想问一下select max(id) from (select top 10 * from A) as t 在其它数据库中的显示结果如何,如MYSQL,如果不一样应该以谁为准?
换个思路求最大行吗?
----------------------------------
这个没有以谁为准的 不同的数据库 即使使用完全相同的ANSI SQL 92标准语法 因为各自的编译存在差异 QUERY的结果也可能不同
select 110,'aaa',600
union all
select 112,'aaa',700
union all
select 120,'aaa',500
union all
select 800,'aaa',400
union all
select 760,'aaa',200
union all
select 1,'aaa',100
union all
select 200,'sss',300
union all
select 150,'www',120
union all
select 400,'eee',180
union all
select 600,'eee',70
1 字段上有索引
select max(id) as id from (select top 3 * from t1) t
select top 3 * from t1
id
600
id name tele
1 aaa 100
150 www 120
200 sss 3002 字段上无索引
select max(tele) as tele from (select top 3 * from t1) t
select top 3 * from t1
tele
300
id name tele
1 aaa 100
150 www 120
200 sss 300结论 top不带order by或索引提示,则按照表的聚集索引排序取数据,否则按照排序字段或索引排序例
create index ix on t1(tele desc)则,对比
select top 3 * from t1 with(index=ix)
select top 3 * from t1select max(id) as id from (select top 3 * from t1 with(index=ix)) t
select max(id) as id from (select top 3 * from t1 )t注:带有max操作的语句在执行前会先根据索引排序自己测试看,结果就不发了
查询select top 3 * from t1 结果不与你一样,我的是:id name tele
1 aaa 100
110 aaa 600
112 aaa 700执行下面一句,结果为700,也与你的不一样select max(tele) as tele from (select top 3 * from t1) t
我用的是sql2000