工作日(日期)与工作日的当前日期进行比较

时间:2014-03-26 08:12:32

标签: mysql

SELECT 
  * 
FROM
  (`users`) 
  JOIN `artistprofiles` AS art 
    ON `art`.`user_id` = `users`.`id` 
WHERE WEEKDAY(users.date_created) = 'WEEKDAY(WEEKDAY(NOW()))' 
  AND `users`.`status_enum` = 'aproved' 
  AND `users`.`status` = 1 
ORDER BY `users`.`email_process` DESC 

此查询有什么问题。它给了我错误的结果

2 个答案:

答案 0 :(得分:1)

试试这样:

SELECT * FROM (`users`) JOIN `artistprofiles` AS art ON `art`.`user_id` = `users`.`id` WHERE WEEKDAY(users.date_created) = WEEKDAY(NOW()) AND `users`.`status_enum` = 'aproved' AND `users`.`status` = 1 ORDER BY `users`.`email_process` desc

'使其成为一个字符串。所以你没有得到工作日。

您可能需要阅读此内容:When to use single quotes, double quotes, and backticks in MySQL

答案 1 :(得分:1)

这是错误的:

WHERE WEEKDAY(users.date_created) = 'WEEKDAY(WEEKDAY(NOW()))' 

示例

select 
    @wd:=WEEKDAY(curdate()) wd, 
    @str:='WEEKDAY(WEEKDAY(NOW()))' str, 
    @wd=@str,
    @wd=WEEKDAY( now() ) ;

结果

+------+-------------------------+----------+----------------------+
| wd   | str                     | @wd=@str | @wd=WEEKDAY( now() ) |
+------+-------------------------+----------+----------------------+
|    2 | WEEKDAY(WEEKDAY(NOW())) |        0 |                    1 |
+------+-------------------------+----------+----------------------+

在您的查询中,您将工作日值与文字字符串'WEEKDAY(WEEKDAY(NOW()))'进行比较。由于它们不匹配,它会返回 false ,因此您会看到错误的结果。

要检查列的工作日是否为现在的工作日,那么

更改

WHERE WEEKDAY(users.date_created) = 'WEEKDAY(WEEKDAY(NOW()))' 

WHERE WEEKDAY(users.date_created) = WEEKDAY( NOW() )