我正在尝试在MySQL中执行IF功能,我认为它非常简单,但似乎有点复杂。
以下是这个想法:
SELECT IF(score1>score2, (name1,score1,name2,score2),(name2,score2,name1,score1))
AS (winner,w_score,loser,l_score)
FROM game table
我要做的是测试得分1>得分2,取决于结果,订购我想要的字段。
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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
这不是最漂亮的事情,但由于比较非常简单,它应该表现得很好。