简体   繁体   中英

MySql: How to get the next record from given condition

If there're two tables, one is players, the other is onfield.

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

How do I find the next player form a given gid? I'm looking for some statement like:

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

If gid=20, the answer is uid: 37, name: james

If gid=38, the answer is uid: 10, name: jack

Question is, I have to get the next record from the statement, and in the case of "last_player='mark'", I have the get the first record.

Is there a way to solve this inside a SQL query?

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

if o.last_player == mark

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM