我的MYSQL QUERY有什么问题?

时间:2012-03-17 13:50:35

标签: mysql

有人可以告诉我我的查询有什么问题。 如果周末从周日下午6点到周五晚上9点设置为“0”,我试图从周围的列中获取数据,然后如果周末设置为“1”,则从周五晚上9点到周日下午6点显示。 / p>

SELECT * 
FROM closures 
WHERE closures.weekend = 0 
      OR WEEKDAY(NOW()) < 4 
      AND closures.weekend = 1 
      OR WEEKDAY(NOW()) > 4 
      OR (WEEKDAY(NOW())=4 AND HOUR(NOW())>21) 
      OR (WEEKDAY(NOW())=6 AND HOUR(NOW())<18)

3 个答案:

答案 0 :(得分:2)

正确地说出问题很有帮助。您应该说的是“星期五晚上9点到星期日下午6点我希望显示closures.weekend = 1的行,否则显示closures.weekend = 0 的行。

因此,您需要做的是生成10的值,具体取决于是否是周末,然后是SELECT weekend所有行那个价值,即:

SELECT *
FROM
    closures
WHERE
    weekend = IF(
         (WEEKDAY(NOW()) = 4 AND HOUR(NOW()) >= 21)
      OR (WEEKDAY(NOW()) = 5)
      OR (WEEKDAY(NOW()) = 6 AND HOUR(NOW()) < 18)
    , 1, 0)

答案 1 :(得分:0)

周末不能同时为0和1

SELECT *
FROM
  closures
WHERE

  closures.weekend = 0
  OR 
  ( 
  WEEKDAY(NOW()) < 4
  AND closures.weekend = 1
  )
  OR WEEKDAY(NOW()) > 4
  OR
(WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR
(WEEKDAY(NOW())=6 AND HOUR(NOW())<18)

答案 2 :(得分:-2)

我的第一个猜测是

SELECT field1,field2,...

缺少?