select * from tb t where not exists(select 1 from tb where name=t.name where ID>t.id)
select * from tb t where times=(select max(times) from tb where name=t.name)
select t.* from tb t where times = (select max(times) from tb where name = t.name)select t.* from tb t where not exists (select 1 from tb where name = t.name and times > t.times)
select * from tb t where not exists(select 1 from tb where TIME>T.name where ID=t.id)
怎么都用到 id 和times了, 现在不能用到这两个列了我记得好像数据库自己有个行什么的可以识别新添加的行,可是不知道是什么了 大家帮帮忙···
数据库是sqlserver 2000create table users ( id int identity(1,1) primary key not null, name varchar(30) not null, times datetime not null )insert into users values('小芳','2009-12-25') insert into users values('小和','2009-12-26') insert into users values('小红','2009-12-26') insert into users values('小东','2009-12-28') insert into users values('洋洋','2009-12-29') insert into users values('星星','2009-12-29')现在不用id(自动增长主键列),times(纪录插入时的系统时间)两个列的信息查询最新记录怎么弄???
CREATE TABLE users ( id INT IDENTITY(1,1) PRIMARY KEY NOT NULL, name VARCHAR(30) NOT NULL, times DATETIME NOT NULL, TIMESTAMP ) GO insert into users (name,times) values('小芳','2009-12-25') insert into users (name,times) values('小和','2009-12-26') insert into users (name,times) values('小红','2009-12-26') insert into users (name,times)values('小东','2009-12-28') insert into users (name,times)values('洋洋','2009-12-29') insert into users (name,times)values('星星','2009-12-29') GO select top 1 * from users order by TIMESTAMP desc
我要得到的结果就是最新插入的一条数据,就是最新数据 比如我刚刚插入了一跳数据 insert into users values('笨笨','2009-12-16') 查询后结果就应该是 6 笨笨 2009-12-16
where not exists(select 1 from tb where name=t.name where ID>t.id)
*
from
tb t
where
times=(select max(times) from tb where name=t.name)
where not exists(select 1 from tb where TIME>T.name where ID=t.id)
大家帮帮忙···
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
(
id int identity(1,1) primary key not null,
name varchar(30) not null,
times datetime not null
)insert into users values('小芳','2009-12-25')
insert into users values('小和','2009-12-26')
insert into users values('小红','2009-12-26')
insert into users values('小东','2009-12-28')
insert into users values('洋洋','2009-12-29')
insert into users values('星星','2009-12-29')现在不用id(自动增长主键列),times(纪录插入时的系统时间)两个列的信息查询最新记录怎么弄???
CREATE TABLE users
(
id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
name VARCHAR(30) NOT NULL,
times DATETIME NOT NULL,
TIMESTAMP
)
GO
insert into users (name,times) values('小芳','2009-12-25')
insert into users (name,times) values('小和','2009-12-26')
insert into users (name,times) values('小红','2009-12-26')
insert into users (name,times)values('小东','2009-12-28')
insert into users (name,times)values('洋洋','2009-12-29')
insert into users (name,times)values('星星','2009-12-29')
GO
select top 1 * from users order by TIMESTAMP desc
我要得到的结果就是最新插入的一条数据,就是最新数据
比如我刚刚插入了一跳数据
insert into users values('笨笨','2009-12-16')
查询后结果就应该是
6 笨笨 2009-12-16
SCOPE_IDENTITY( ):
返回插入到同一作用域中的 IDENTITY 列内的最后一个 IDENTITY 值。一个作用域就是一个模块——存储过程、触发器、函数或批处理。因此,如果两个语句处于同一个存储过程、函数或批处理中,则它们位于相同的作用域中。
使用:
SELECT SCOPE_IDENTITY()
-----------------------------------------------------------------------------------------------
IDENT_CURRENT('table_name')
返回为任何会话和任何作用域中的指定表最后生成的标识值。
使用:
SELECT IDENT_CURRENT('table_name')
*
from
tb t
where
exists (select max(t1.times) from tb t1 where t.times = t1.times)