数据表:
Number(int) Name(char) sex
1 a 男
2 b 女
3 b 男
4 a 女
5 c 男
6 c 女
第一步得到这种形式sql语句已经写好了:
Number(int) Name(char)
1 a
2 b
5 c
语句如下:
select * from tt a
where not exists(select * from tt where name=a.name and number<a.number)
第二步是查询第一步其中一条数据:
Number(int) Name(char)
1 a
怎么只用用一条sql语句写出来啊?知道的帮忙解答下!!!
Number(int) Name(char) sex
1 a 男
2 b 女
3 b 男
4 a 女
5 c 男
6 c 女
第一步得到这种形式sql语句已经写好了:
Number(int) Name(char)
1 a
2 b
5 c
语句如下:
select * from tt a
where not exists(select * from tt where name=a.name and number<a.number)
第二步是查询第一步其中一条数据:
Number(int) Name(char)
1 a
怎么只用用一条sql语句写出来啊?知道的帮忙解答下!!!
where not exists(select * from tt where name=a.name and number<a.number)
order by newid()
select top 1 * from tt a
where not exists(select * from tt where name=a.name and number<a.number)取Number最小第一条
select top 1 * from tt a
where not exists(select * from tt where name=a.name and number<a.number)
order by Number
select * from
(
select * from tt a
where not exists(select * from tt where name=a.name and number<a.number)
) a
where a.number = 1
楼主是这个意思么
select * from tt a
where not exists(select * from tt where name=a.name and number<a.number)and name='a'
后面加个and条件就行了