T表
FILE1 FILE1 FILE1
1 2 1
2 3 1
3 4 1
4 4 3
5 4 5需要的结果FILE1 FILE1 FILE1
1 2 1
2 3 1
5 4 5
怎么写这个查询
FILE1 FILE1 FILE1
1 2 1
2 3 1
3 4 1
4 4 3
5 4 5需要的结果FILE1 FILE1 FILE1
1 2 1
2 3 1
5 4 5
怎么写这个查询
解决方案 »
- 一个关于列问题~
- 求最低单价的单据日期和号码?
- 大家推荐个数据库视频哈
- 有一个表结构见里,表结构比较简单,请问如何求出下面的结果,具体请见里,不知如何写sql语句,万分感谢!
- 表的连接问题,连接条件是一个字符串分拆成的多个字符串只要其中任意一个字符串是另一个表相应字段的子串就应该匹配
- 急问题:还原SQL数据库!
- T-SQL: 关于 Varbinary(Hex,Int) 与 Varchar(HexString) 之间的(数据类型)转换
- 一个SQL语句的问题,麻烦各位大大了,谢谢!
- 怎样用select语句找到,<null>的数据?
- 求助:合并完全不同的两个表的查询结果,再把合并后的结果进行分页。有代码
- 数据放在最上面,并加编号
- 这个查询怎么写
T表
FILE1 FILE1 FILE1
1 2 1
2 3 1
3 4 1
4 4 3
5 4 5
---------------------
字段一样?还是改下字段吧。T表
FILE1 FILE2 FILE3
1 2 1
2 3 1
3 4 1
4 4 3
5 4 5 select * from t a where not exists(select 1 from t where file2=a.file2 and file3>a.file3)
--或者:
select * from t a where file3 in (select max(file3) from t where file2=a.file2)
--等等
go
insert into t
select 1,2,1
union all select 2,3,1
union all select 3,4,1
union all select 4,4,3
union all select 5,4,5
go
select * from t
select * from t where not exists(select 1 from t t2 where t2.file2=t.file2 and t2.file3 >t.file3)
go
drop table t;
go
FILE1 FILE2 FILE3
1 2 1
2 3 1
3 4 1
4 4 3
5 4 5 需要的结果 FILE1 FILE2 FILE3
1 2 1
2 3 1
5 4 5
怎么写这个查询
改字段为1,2,3
select m.* from t m where file1 = (select max(file1) from t where file2 = m.file2)
FILE1 FILE2 FILE3
101 A 1
201 B 1
301 C 1
401 C 3
501 C 5 结果
FILE1 FILE2 FILE3
101 A 1
201 B 1 501 C 5
go
insert into t
select '101','A',1
union all select '201','B',1
union all select '301','C',1
union all select '401','C',3
union all select '501','C',5
go
select * from t;
select * from t where not exists(select 1 from t t2 where t2.file2=t.file2 and t2.file3>t.file3)
go
drop table t;
go
/*
结果
FILE1 FILE2 FILE3
101 A 1
201 B 1 501 C 5 */(所影响的行数为 5 行)FILE1 FILE2 FILE3
-------------------- -------------------- -----------
101 A 1
201 B 1
301 C 1
401 C 3
501 C 5(所影响的行数为 5 行)FILE1 FILE2 FILE3
-------------------- -------------------- -----------
101 A 1
201 B 1
501 C 5(所影响的行数为 3 行)
--这样也可以
select *
from t
where file3=(select max(file3) from t t2 where t2.file2=t.file2)
order by file1