0

I'm getting an error that I'm not being able to solve. I'm rather new at SQL, so I imagine this is quite an idiotic error, but I'd really appreciate somebody's help with this.

I'm trying to see if there are rows in a certain table that were created in the current week (From monday on). So I wrote this.

SELECT 
  *, 
  DATE_SUB(CURRENT_DATE,INTERVAL ( DAYOFWEEK(CURRENT_DATE) - 2 ) DAY) AS `week_start`,
  CASE WHEN `ganador`.`fecha` >= `week_start` THEN 0 ELSE 1 END AS `this_week`
FROM `lesaffre_ganadores` as `ganador`

I'm getting the following error:

1054 - Unknown column 'week_start' in 'field list'

I assume there is a way to define the variable week_start in order to be able to use it in the query, but I really ignore it.

Could anyone help me?

Thank you very much!

1 Answer 1

1
SELECT 
*,
CASE WHEN `ganador`.`fecha` >= `week_start` THEN 0 ELSE 1 END AS `this_week`

FROM
(
SELECT 
  *, 
  DATE_SUB(CURRENT_DATE,INTERVAL ( DAYOFWEEK(CURRENT_DATE) - 2 ) DAY) AS `week_start`
FROM `lesaffre_ganadores` 
)ganador
Sign up to request clarification or add additional context in comments.

1 Comment

This worked really well. I get what my error was. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.