从日期范围但按区域获取数据

时间:2018-09-13 18:15:59

标签: mysql sql

我正在尝试从日期范围以及办公室面积等于office->id area的地方获取数据。这是我的查询

SELECT * FROM office as d
      LEFT JOIN office_area as e ON e.ID_Area = d.ID_Area
      WHERE d.ID_Area = 1 OR d.ID_Area = 6
      AND d.date BETWEEN '2018-09-01' AND '2018-09-30'
    ORDER BY d.date DESC

问题在于获取日期为2018-01-01的数据是因为第一个位置。如何才能从该日期范围的16区域中获取数据

1 个答案:

答案 0 :(得分:0)

您需要添加一些括号以对逻辑进行排序:

SELECT * FROM office as d
  LEFT JOIN office_area as e ON e.ID_Area = d.ID_Area
  WHERE (d.ID_Area = 1 OR d.ID_Area = 6)
  AND (d.date BETWEEN '2018-09-01' AND '2018-09-30')
ORDER BY d.date DESC