从两个date之间的date范围中select数据
我有一个名为Product_Sales
的表,它拥有这样的数据
Product_ID | Sold_by | Qty | From_date | To_date -----------+---------+-----+------------+----------- 3 | 12 | 7 | 2013-01-05 | 2013-01-07 6 | 22 | 14 | 2013-01-06 | 2013-01-10 8 | 11 | 9 | 2013-02-05 | 2013-02-11
现在,如果我想从date范围的两个date之间select销售数据,那么查询是什么?
例如,我想从2013-01-03
到2013-01-09
select销售数据。
正如你所看到的,有两种方法可以完成任务:
- 征集所有可以接受的选项
- 排除所有错误的选项
显然,第二种方式更为简单(只有两种情况,四种)。
你的SQL将如下所示:
SELECT * FROM Product_sales WHERE NOT (From_date > @RangeTill OR To_date < @RangeFrom)
尝试以下查询来获取范围之间的date:
SELECT * FROM Product_sales WHERE From_date >= '2013-01-03' AND To_date <= '2013-01-09'
SELECT * from Product_sales where From_date BETWEEN ('2013-01-03', '2013-01-09') OR To_date BETWEEN ('2013-01-03', '2013-01-09') OR From_date <= '2013-01-03' AND To_date >= '2013-01-09'
如果其中一个From_date
或From_date
在date之间,或者From_date
小于开始date,并且To_date
大于结束date。
SELECT * FROM Product_sales WHERE From_date between '2013-01-03' AND '2013-01-09'
请尝试:
DECLARE @FrmDt DATETIME, @ToDt DATETIME SELECT @FrmDt='2013-01-03', @ToDt='2013-01-09' SELECT * FROM Product_sales WHERE (@FrmDt BETWEEN From_date AND To_date) OR (@ToDt BETWEEN From_date AND To_date)
SELECT * FROM Product_sales WHERE ( From_date >= '2013-08-19' AND To_date <= '2013-08-23' ) OR ( To_date >= '2013-08-19' AND From_date <= '2013-08-23' )
select * from table where ( (table.EndDate > '2013-01-05') and (table.StartDate < '2013-01-07' ) )
这在SQL_Server_2008 R2上工作
Select * from Product_sales where From_date between '2013-01-03' and '2013-01-09'
这个查询将帮助你:
select * from XXXX where datepart(YYYY,create_date)>=2013 and DATEPART(YYYY,create_date)<=2014
您也可以尝试使用以下片段:
select * from Product_sales where From_date >= '2013-01-03' and game_date <= '2013-01-09'
你应该在sql中比较date,就像比较数字值一样,
SELECT * FROM Product_sales WHERE From_date >= '2013-01-01' AND To_date <= '2013-01-20'
只是我的2分钱,我发现使用“dd-MMM-yyyy”格式最安全,因为无论服务器上的区域设置如何,数据库服务器都会知道你想要什么。 否则,如果服务器的date区域设置为yyyy-dd-mm,则可能会遇到问题(无论出于何种原因)
从而:
SELECT * FROM Product_sales WHERE From_date >= '03-Jan-2013' AND To_date <= '09-Jan-2013'
这对我来说一直很好;-)
以下是查询8月份所有正在运行的产品销售情况的查询
- 在8月份查找Product_sales活动
- 包括8月底之前开始的任何事情
- 排除8月1日之前结束的任何事情
还添加了一个case语句来validation查询
SELECT start_date, end_date, CASE WHEN start_date <= '2015-08-31' THEN 'true' ELSE 'false' END AS started_before_end_of_month, CASE WHEN NOT end_date <= '2015-08-01' THEN 'true' ELSE 'false' END AS did_not_end_before_begining_of_month FROM product_sales WHERE start_date <= '2015-08-31' AND end_date >= '2015-08-01' ORDER BY start_date;
SELECT NULL FROM HRMTable hm(NOLOCK) WHERE hm.EmployeeID = 123 AND ( ( CAST(@Fromdate AS date) BETWEEN CAST(hm.FromDate AS date) AND CAST(hm.ToDate AS date) ) OR ( CAST(@Todate AS date) BETWEEN CAST(hm.FromDate AS date) AND CAST(hm.ToDate AS date) ) ) )
检查这个查询,我创build了这个查询,以检查是否在任何保留date在检查date圈
SELECT * FROM tbl_ReservedRooms WHERE NOT ('@checkindate' NOT BETWEEN fromdate AND todate AND '@checkoutdate' NOT BETWEEN fromdate AND todate)
这将重复正在重叠的细节,以获得不重叠的细节,然后从查询中删除“NOT”
这个代码将很好地工作:
控制器:
$this->load->model("YourModelName"); $data ['query'] = $this->YourModelName->get_report();
模型:
public function get_report() { $query = $this->db->query("SELECT * FROM reservation WHERE arvdate <= '2016-7-20' AND dptrdate >= '2016-10-25' "); return $query; }
其中'arvdate'和'dptrdate'是数据库上的两个date,'reservation'是表名。
视图:
<?php echo $query->num_rows(); ?>
这段代码是返回行数。 要返回表数据,然后使用
$query->rows(); return $row->table_column_name;