帮忙写个SQL简单的

表结构:(tb)
记录编号(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

(简单解释一下:)
同一个棋局,很多用户会玩,每玩一次,会有一个分数保存。
name=“我”,玩过很多局,我要搜索出,所有我玩过的棋局中,最高分记录不是我的那些棋局。

select * from tb where name=我

这些就是我玩过的局

作者: qlj8012   发布时间: 2011-06-09

那我怎么知道最高分是什么呢

作者: rulioo   发布时间: 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)

作者: zy112429   发布时间: 2011-06-09

SQL code
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

MAYBE.
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

SQL code

--棋局号(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

引用 5 楼 zy112429 的回复:
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

试试7、8楼,没测试。

作者: rulioo   发布时间: 2011-06-09

引用 10 楼 rulioo 的回复:
搜索结果应该是-- 订正
1,abc,100,rulioo
5,hhh,100,rulioo
SQL code
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

SQL code

--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

SQL code

--还是这个
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

select * from tb a
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

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   发布时间: 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

引用 17 楼 x_0 的回复:
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

引用 19 楼 yes20090314 的回复:
那我怎么知道最高分是什么呢


可以知道,也可以不用知道最高分是多少,

只要“同一局号下的成绩,有比我的成绩分数高,但是那个高分的用户姓名不是我”

7,8楼给的方案,好像没法剔除,这种情况,就是,

“发现,比我高的成绩,但是其实,那个成绩也是我的”
也就是,没法筛选

e.g.

8,jjj,100,rulioo
9,jjj,110,rulioo

sql结果
8,jjj,100,rulioo

作者: rulioo   发布时间: 2011-06-09

引用 8 楼 acherat 的回复:
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