2012-01-16 136 views
0

如果有兩個表,一個是玩家,另一個是現場。MySql:如何從給定條件獲取下一條記錄

表::球員:

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

表::遊戲

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,店名:詹姆斯

如果GID = 38,答案是UID:10,店名:插孔

問題是,我必須從聲明中獲取下一條記錄,並且在「last_player ='標記'」的情況下,我得到第一條記錄。

有沒有辦法解決這個SQL查詢內?

回答

0

如果o.last_player!=標記

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 ==標誌

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