尝试使用临时列时出错,mysql

时间:2013-09-14 03:09:42

标签: mysql

我收到此错误,

#1054 - Unknown column 'finalResult' in 'field list'

什么时候我运行这个mysql查询

SELECT userID, pickID, nflp_picks.gameID, visitorID, visitorResult, homeID, homeResult,

IF (pickID=visitorID, visitorResult, homeResult) AS finalResult,

IF (finalResult="Winner", 1, 0) AS Win

FROM nflp_picks, nflp_schedule

WHERE nflp_picks.gameID = nflp_schedule.gameID

ORDER BY gameID, pickID, userID

LIMIT 0, 200

我可以不使用临时列吗?

1 个答案:

答案 0 :(得分:2)

除了GROUP BY,ORDER BY和HAVING子句之外,您不能在同一查询中引用别名。您可以使用子查询:

SELECT *, (finalResult = "Winner") AS Win
FROM (SELECT userID, pickID, nflp_picks.gameID, visitorID, visitorResult, homeID, homeResult,
             IF (pickID=visitorID, visitorResult, homeResult) AS finalResult
      FROM nflp_picks
      JOIN nflp_schedule
      ON nflp_picks.gameID = nflp_schedule.gameID
      ORDER BY gameID, pickID, userID
      LIMIT 0, 200) x