MySql:如何从给定条件中获取下一条记录

时间:2012-01-16 16:03:30

标签: mysql

如果有两张桌子,一张是球员,另一张是球场。

table :: player:

uid:10, name:jack
uid:23, name:david
uid:37, name:james
uid:42, name:nick
uid:58, name:mark

table :: games

gid: 20, last_player: david
gid: 32, last_player: jack
gid: 38, last_player: mark
gid: 43, last_player: nick

如何从给定的gid中找到下一位玩家? 我正在寻找一些声明:

SELECT p.uid, p.name FROM player p AND onfield o WHERE o.last_player = p.name AND gid=20 ORDER BY uid ASC;

如果gid = 20,答案是uid:37,name:james

如果gid = 38,答案是uid:10,name:jack

问题是,我必须从声明中获取下一条记录,而在“last_player ='mark'”的情况下,我得到第一条记录。

有没有办法在SQL查询中解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果o.last_player!= mark

   SELECT p.uid, p.name FROM player p, onfield o WHERE o.last_player = p.name 
   AND p.uid > o.guid ORDER BY uid ASC limit 1;

如果o.last_player == mark

  SELECT p.uid, p.name FROM player p, onfield o ORDER BY p.uid ASC limit 1;