SQL语句 之前问题延伸
时间:2011-12-10
来源:互联网
EMPTable
id name age gender createdate
1 张三 15 男 2011-12-5
2 张三 16 男 2011-12-6
3 张三 15 男 2011-12-9
4 李四 14 女 2011-12-3
5 李四 14 女 2011-12-15
查询 某人 最后一次创建的时间.(条件是姓名和年龄,出来的结果以id排序)
现在出来id很乱.我直接在
select * from tb t whrre createtime=(select max(createtime) from tb where name=t.name amd age=t.age)后面加order by id 查不来也不对的.
我想知道数据库循环怎么会把id打乱了.什么原理.
正确显示:
id name age gender createdate
3 张三 15 男 2011-12-9
2 张三 16 男 2011-12-6
5 李四 14 女 2011-12-5
之前感谢
ssp2009
fredrickhu
id name age gender createdate
1 张三 15 男 2011-12-5
2 张三 16 男 2011-12-6
3 张三 15 男 2011-12-9
4 李四 14 女 2011-12-3
5 李四 14 女 2011-12-15
查询 某人 最后一次创建的时间.(条件是姓名和年龄,出来的结果以id排序)
现在出来id很乱.我直接在
select * from tb t whrre createtime=(select max(createtime) from tb where name=t.name amd age=t.age)后面加order by id 查不来也不对的.
我想知道数据库循环怎么会把id打乱了.什么原理.
正确显示:
id name age gender createdate
3 张三 15 男 2011-12-9
2 张三 16 男 2011-12-6
5 李四 14 女 2011-12-5
之前感谢
ssp2009
fredrickhu
作者: CCXY_CPP 发布时间: 2011-12-10
SQL code
select a.* from EMPTable a inner join (select name,age,max(createdate) mcreatedate from EMPTable group by name,age) b on a.name=b.name and a.age=b.age and a.createdate=b.mcreatedate order by a.id
作者: ap0405140 发布时间: 2011-12-10
SQL code
IF NOT OBJECT_ID('tb') IS NULL DROP TABLE tb create table tb (id int, [name] varchar(50),age int ,gender varchar(50),createdate datetime) insert into tb select '1','张三', 15 ,'男' ,'2011-12-5' insert into tb select '2','张三', 16 ,'男' ,'2011-12-6' insert into tb select '3', '张三', 15 ,'男' ,'2011-12-9' insert into tb select '4','李四', 14 ,'女' ,'2011-12-3' insert into tb select '5', '李四', 14 ,'女' ,'2011-12-15' select * from tb a where not exists(select 1 from tb b where b.createdate>a.createdate and a.age=b.age) id name age gender createdate ----------- -------------------------------------------------- ----------- -------------------------------------------------- ----------------------- 2 张三 16 男 2011-12-06 00:00:00.000 3 张三 15 男 2011-12-09 00:00:00.000 5 李四 14 女 2011-12-15 00:00:00.000 (3 行受影响)
作者: myselfff 发布时间: 2011-12-10
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28