1

我正在尝试在 MySQL 中执行一个我认为非常简单的 IF 函数,但似乎有点复杂。

这是想法:

SELECT IF(score1>score2, (name1,score1,name2,score2),(name2,score2,name1,score1)) AS (winner,w_score,loser,l_score) FROM game table

我要做的是测试 score1 > score2,根据结果,对我想要的字段进行排序。

4

3 回答 3

1

考虑如果score1, score2, name1,name2是表中的列名game,您可以这样做:

SELECT
     IF( score1 > score2 ,name1 ,name2 ) AS winner
    ,GREATEST( score1 ,score2 ) AS w_score
    ,IF( score1 > score2 ,name2 ,name1 ) AS loser
    ,LEAST( score1 ,score2 ) AS l_score
FROM
    game
于 2013-11-14T03:39:17.053 回答
0

我提供一个简单的解决方案:

SELECT name1 AS winner, score1 AS w_score, name2 AS looser, score2 AS l_score
FROM game table WHERE score1 > score2
UNION
SELECT name2 AS winner, score2 AS w_score, name1 AS looser, score1 AS l_score
FROM game table WHERE score2 > score1
于 2013-11-14T03:16:43.463 回答
0

看起来您需要的是四个不同的 IF 语句:

SELECT 
    IF(score1 > score2, name1, name2) AS winner,
    IF(score1 > score2, score1, score2) AS w_score,
    IF(score1 > score2, name2, name1) AS loser,
    IF(score1 > score2, score2, score1) AS l_score
FROM game_table

这不是最漂亮的东西,但由于比较非常简单,它应该表现得很好。

于 2013-11-14T03:18:46.457 回答