0

I am getting this error,

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

when ever I run this mysql query

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

Can I not use temporary columns?

4

1 回答 1

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
于 2013-09-14T03:17:00.403 回答