Mysql查询无法使用WHERE BETWEEN

时间:2014-06-05 11:46:32

标签: mysql where between

我试图从我的数据库中获取一些数据,但我目前只是收到错误。

我正在尝试的查询=

SELECT 
   count(id), 
   day(created_at), 
   year(created_at), 
   month(created_at) 
FROM 
   `orders` 
WHERE 
   day(created_at) = BETWEEN 1 AND 7 month(created_at) = 6 
   AND year(created_at) = 2014 
   AND company_id = 1 
group by 
   year(created_at), 
   month(created_at), 
   day(created_at)

1到7之间的日子将是星期六的星期日,以获得该周的所有订单。

提前致谢。

5 个答案:

答案 0 :(得分:1)

您的查询错误。这应该是:

  SELECT count(id), day(created_at), year(created_at), month(created_at) 
      FROM `orders` WHERE day(created_at) BETWEEN 1 AND 7 
            AND month(created_at) = 6 AND year(created_at) = '2014' 
            AND company_id = 1 
        group by year(created_at), month(created_at), day(created_at)

你在同一个地方使用=和BETWEEN是不正确的。

答案 1 :(得分:1)

SELECT count(id), day(created_at), year(created_at), month(created_at) 
FROM `orders` 
WHERE day(created_at) BETWEEN 1 AND 7
AND month(created_at) = 6 
AND year(created_at) = 2014 
AND company_id = 1 
group by year(created_at), month(created_at), day(created_at)

错过AND之前的month(created_at)

答案 2 :(得分:0)

month(created_at)=

附近的查询语法错误
SELECT count(id),
       day(created_at),
       year(created_at),
       month(created_at)
FROM `orders`
WHERE day(created_at) BETWEEN 1 AND 7 
  AND month(created_at) = 6
  AND year(created_at) = 2014
  AND company_id = 1
GROUP BY year(created_at),
         month(created_at),
         day(created_at)

答案 3 :(得分:0)

你必须从你的查询中修剪=  我再次为你写信:

SELECT count(id), day(created_at), year(created_at), month(created_at) 
FROM `orders`
WHERE (day(created_at) BETWEEN 1 AND 7)
AND (month(created_at) = 6)
AND (year(created_at) = 2014)
AND (company_id = 1)
group by year(created_at), month(created_at), day(created_at)

答案 4 :(得分:0)

   You have SQL Syntax errors , MISSING "AND" and "BETWEEN" not used correctly.
   Try the following:-


    SELECT COUNT(id), day(created_at), year(created_at), month(created_at)
    FROM  
   `orders`
    WHERE day(created_at)  BETWEEN 1 AND 7 
    AND  month(created_at) = 6 
    AND year(created_at) = 2014
    AND company_id = 1 
    GROUP BY year(created_at), month(created_at), day(created_at);