Link to home
Start Free TrialLog in
Avatar of Colin Brazier
Colin BrazierFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Unknown column when using CASE in MySQL query

After 14 years,  my 300th question...

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;

Open in new window


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?
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Have you tried specifically listing the columns? (i.e. not using "*")

Does the union query operate if run by itself without creating a table?
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
Norie

Should line 16 not just be CASE instead of CASE wdl?
Avatar of Colin Brazier

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
Thanks - I said it was late!  That was a result of not remembering how CASE worked and the legacy of an earlier effort.  Cheers.