我的问题的示例架构在SQLFiddle中给出,如下所示:
CREATE TABLE `players` ( `player1id` int(11) NOT NULL, `player2id` int(11) NOT NULL, PRIMARY KEY (`player1id`,`player2id`) ) ENGINE=InnoDB; INSERT INTO `players` values (1,5), (1,7), (5,3), (5,4), (2,1);
在在线游戏中,我想选择对手,该对手可以在玩家player1id或中player2id。
player1id
player2id
所需的示例输入/输出
Input 1: Output 5, 7, 2 Input 5: Output 1, 3 ,4 & so on.
也就是说,所需的数据可以在任何列中,但是我需要有条件地或以任何其他方式在单列中输出。我听说过MySQL条件列,但无法创建查询以获取所需的输出。有人可以帮助您进行必要的查询吗?
编辑
基于此链接,我运行以下查询,但失败。
SELECT IF(PLAYER1ID IS 1,PLAYER2ID as opponent,PLAYER1ID as opponent) FROM players WHERE PLAYER1ID = 1 OR PLAYER2ID = 1;
我认为您可以使用case when then以下语法:
case when then
SELECT CASE WHEN player1id = 1 THEN player2id ELSE player1id END FROM players WHERE player1id =1 OR player2id=1;
或者:
SELECT CASE WHEN player1id = 1 THEN player2id WHEN player2id =1 THEN player1id END FROM players WHERE player1id =1 OR player2id=1;