0

This is not working. My column names include Mon, Tue, Wed, Thu, Fri, Sat, Sun set to 0 or 1. I am trying to see if column name is 0 or 1 based on the 3-digit current day of the week.

SET @var = DATE_FORMAT(NOW(), '%a') 
SELECT *, @var 
FROM team LEFT JOIN fav ON team.Tag = fav.Tag WHERE ID = '123456' AND @var = '1' 
ORDER BY team.Name
7
  • Why do you need the SET? Why not just put that in the condition? Commented Nov 12, 2019 at 20:49
  • I initially tried $var=date('D'); WHERE $var='1' but that did not work Commented Nov 12, 2019 at 20:50
  • You can't dynamically select a column name. That's equivalent to SELECT *, '1' for Monday. Consider making your data properly relational so you can do this query without a lot of fuss. Commented Nov 12, 2019 at 20:50
  • Can you give me an example? Commented Nov 12, 2019 at 20:54
  • You'll need to look up database normal forms and better understand how to relate your data to make this query more concise. It should be a one-to-many relationship between one row and the various on-day-X table with other rows. Commented Nov 12, 2019 at 20:55

1 Answer 1

2

The variable's @var value is a string literal and can't be used as a column.
You can do what you want with a CASE expression:

SET @var = DATE_FORMAT(NOW(), '%a'); 
SELECT *, @var 
FROM team LEFT JOIN fav 
ON team.Tag = fav.Tag 
WHERE ID = '123456' 
AND '1' = CASE @var
  WHEN 'Mon' THEN Mon
  WHEN 'Tue' THEN Tue
  WHEN 'Wed' THEN Wed
  WHEN 'Thu' THEN Thu
  WHEN 'Fri' THEN Fri
  WHEN 'Sat' THEN Sat
  WHEN 'Sun' THEN Sun
END
ORDER BY team.Name;

Or without the variable:

SELECT * 
FROM team LEFT JOIN fav 
ON team.Tag = fav.Tag 
WHERE ID = '123456' 
AND '1' = CASE DATE_FORMAT(NOW(), '%a')
  WHEN 'Mon' THEN Mon
  WHEN 'Tue' THEN Tue
  WHEN 'Wed' THEN Wed
  WHEN 'Thu' THEN Thu
  WHEN 'Fri' THEN Fri
  WHEN 'Sat' THEN Sat
  WHEN 'Sun' THEN Sun
END
ORDER BY team.Name;

Inside the CASE expression qualify the columns Mon, Tue, Wed, Thu, Fri, Sat, Sun with the table (team or fav) they belong.

Sign up to request clarification or add additional context in comments.

5 Comments

The SELECT ..., @var part doesn't really help here, nor the static '1', but the CASE is definitely the way to tackle this problem.
The static '1' is the OP's code from the WHERE clause, so why doesn't it help? The @var maybe is not needed in the select list but it's up to the OP to decide.
I think it's an artifact of some confusion, when really what's intended is SELECT ..., CASE DATE_FORMAT(NOW(), '%a')... instead, or even better, DAYOFWEEK(NOW()) instead of the DATE_FORMAT excursion.
The OP thought that by setting: SET @var = DATE_FORMAT(NOW(), '%a'); then the variable could be used in place where a column like Mon can be used. This is the reason of the use of the variable. Sure the code can be written without it by using a function inside the CASE statement.
Thank you @forpas for the solution to my question. Worked beautifully. I ended up dropping the variable and going with SELECT * FROM team LEFT JOIN fav ON team.Tag = fav.Tag WHERE ID = '123456' AND '1' = CASE DATE_FORMAT(NOW(), '%a') WHEN 'Mon' THEN Mon WHEN 'Tue' THEN Tue WHEN 'Wed' THEN Wed WHEN 'Thu' THEN Thu WHEN 'Fri' THEN Fri WHEN 'Sat' THEN Sat WHEN 'Sun' THEN Sun END ORDER BY team.Name; Thanks again.

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.