帮忙写个SQL简单的
记录编号(id),棋局号(sn),分数(score),姓名(name)
SQL查:
列出所有我玩过但是最高分数不是我的棋局
这个SQL如何写,谢谢
作者: rulioo 发布时间: 2011-06-09
作者: zy112429 发布时间: 2011-06-09
该回复于2011-06-09 17:09:39被管理员删除
- 对我有用[0]
- 丢个板砖[0]
- 引用
- 举报
- 管理
- TOP
|
#3楼 得分:0回复于:2011-06-09 17:10:08
|
作者: qlj8012 发布时间: 2011-06-09
作者: rulioo 发布时间: 2011-06-09
select distinct id from tb a where name='我' and exists(select 1 from tb where id=a.id and score>a.score)
作者: zy112429 发布时间: 2011-06-09
select id,sn,score,name from tb where name = '我' and score not in (select max(score) as score from tb where name <> '我')
作者: zy112429 发布时间: 2011-06-09
SQL code
SELECT * FROM tb A WHERE EXISTS (SELECT 1 FROM tb WHERE sn=A.sn AND score > A.score ) AND [name]='我'
作者: jyh070207 发布时间: 2011-06-09
--棋局号(sn),分数(score),姓名(name) select distinct sn from tb t where score < (select max(score) where sn = t.sn) and name = N'我'
作者: ForFumm 发布时间: 2011-06-09
SQL code
select distinct id from tb a
where name='我' and exists(select 1 from tb where id=a.id and score>a.score)
我的问题是:
如何从这些我玩过的棋局中,筛选保留那些“同一局号最高,但是最高成绩不是我的局”
e.g.
记录编号(id),棋局号(sn),分数(score),姓名(name)
1,abc,100,rulioo
2,abc,120,zy112429
3,ggg,100,rulioo
4,ggg,90,zy112429
5,hhh,100,rulioo
6,hhh,101,zy112429
搜索结果应该是
1,abc,100,rulioo
6,hhh,100,rulioo
作者: AcHerat 发布时间: 2011-06-09
1,abc,100,rulioo
5,hhh,100,rulioo
作者: rulioo 发布时间: 2011-06-09
作者: rulioo 发布时间: 2011-06-09
搜索结果应该是-- 订正
1,abc,100,rulioo
5,hhh,100,rulioo
select * from tb a where name<>'我' and not exists(select 1 from tb where id=a.id and score>a.score)
作者: AcHerat 发布时间: 2011-06-09
--id改为sn select * from tb a where name<>'我' and not exists(select 1 from tb where sn=a.sn and score>a.score)
作者: zy112429 发布时间: 2011-06-09
--还是这个 select * from tb a where name='rulioo' and exists(select 1 from tb where sn=a.sn and score>a.score)
作者: zy112429 发布时间: 2011-06-09
where name<>'我' and not exists(select 1 from tb where id=a.id and score>a.score)
作者: zy112429 发布时间: 2011-06-09
作者: admczy 发布时间: 2011-06-09
select t1.* from tb t1 where exists(select 1 from tb t2 where t1.sn=t2.sn and t2.[name]='我') and not exists (select 1 from tb t3 where t1.sn=t3.sn and t1.score<t3.score)
作者: rulioo 发布时间: 2011-06-09
rulioo玩过的一局,第一,第2 都是他,结果也被选出来了。
e.g.
8,jjj,100,rulioo
9,jjj,110,rulioo
sql结果
8,jjj,100,rulioo
作者: X_0 发布时间: 2011-06-09
作者: rulioo 发布时间: 2011-06-09
SQL code
select t1.*
from tb t1
where exists(select 1 from tb t2 where t1.sn=t2.sn and t2.[name]='我')
and not exists (select 1 from tb t3 where t1.sn=t3.sn and t1.score<t3.score)
结果有点乱,有些是rulioo的记录,还有其他人的
作者: yes20090314 发布时间: 2011-06-09
那我怎么知道最高分是什么呢
可以知道,也可以不用知道最高分是多少,
只要“同一局号下的成绩,有比我的成绩分数高,但是那个高分的用户姓名不是我”
7,8楼给的方案,好像没法剔除,这种情况,就是,
“发现,比我高的成绩,但是其实,那个成绩也是我的”
也就是,没法筛选
e.g.
8,jjj,100,rulioo
9,jjj,110,rulioo
sql结果
8,jjj,100,rulioo
作者: rulioo 发布时间: 2011-06-09
SQL code
--棋局号(sn),分数(score),姓名(name)
select distinct sn
from tb t
where score < (select max(score) where sn = t.sn)
and name = N'我'
选出来的,都是由rulioo保持记录的局,呵呵,正好反了。
作者: rulioo 发布时间: 2011-06-09