Colin Brazier
asked on
Unknown column when using CASE in MySQL query
After 14 years, my 300th question...
Please see this MySQL query
When I run it I get an error 1054 Unknown column 'wdl' in field list.
My immediate response is...Of course it doesn't exist, I'm trying to create it!
What am I missing?
Please see this MySQL query
CREATE TEMPORARY TABLE scores AS
SELECT * FROM
(
SELECT home_team AS Team, home_score AS f_score, away_score AS a_score, match_date,
CASE
WHEN home_score > away_score THEN 'W'
WHEN home_score < away_score THEN 'L'
ELSE 'D'
END AS wdl
FROM all_results
WHERE ar_fobg_team_code = '1'
AND ar_season_start = 2017
AND home_score REGEXP '[[:digit:]]'
UNION
SELECT away_team AS Team, away_score AS f_score, home_score AS a_score, match_date,
CASE wdl
WHEN home_score > away_score THEN 'L'
WHEN home_score < away_score THEN 'W'
ELSE 'D'
END AS wdl
FROM all_results
WHERE ar_fobg_team_code = '1'
AND ar_season_start = 2017
AND home_score REGEXP '[[:digit:]]'
) AS the_data;
When I run it I get an error 1054 Unknown column 'wdl' in field list.
My immediate response is...Of course it doesn't exist, I'm trying to create it!
What am I missing?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Should line 16 not just be CASE instead of CASE wdl?
ASKER
I now use SELECT Team, f_score, a_score, match_date, wdl FROM
and it still fails with the same error whether creating the table or not. It's late, I'll check again tomorrow. Thanks for responding.
Col
and it still fails with the same error whether creating the table or not. It's late, I'll check again tomorrow. Thanks for responding.
Col
ASKER
Thanks - I said it was late! That was a result of not remembering how CASE worked and the legacy of an earlier effort. Cheers.
Does the union query operate if run by itself without creating a table?