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?
LVL 3
Colin BrazierAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Does the union query operate if run by itself without creating a table?
0
PortletPaulEE Topic AdvisorCommented:
Oh I see it now... there was a "wdl" after the case keyword in the send half of your query, now removed
CREATE TEMPORARY TABLE scores AS
SELECT
      team, f_score, a_score,match_date, wdl
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
                  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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
Should line 16 not just be CASE instead of CASE wdl?
1
Colin BrazierAuthor Commented:
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
0
Colin BrazierAuthor Commented:
Thanks - I said it was late!  That was a result of not remembering how CASE worked and the legacy of an earlier effort.  Cheers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.