日期和时间处理函数
1、date() ; time()
如果查询结果需求为日期,应使用date(),即使知道相应的列只包含日期也是如此。如果想要的是时间,应使用time(),这样如果以后由于某种原因有日期和时间值,你的SQL代码也不用改变。
比较:
select cust_id,order_num from orders where order_date = '2005-09-01';
select cust_id,order_num from orders where date(order_date) = '2005-09-01';
若存储的order_date值为2005-09-01 11:30:05 ,则where order_date = '2005-09-01'
失败。
用date()提取日期部分则可匹配。
2、year() ; month()
检索2005年9月的订单
比较:
select cust_id,order_num from orders where date(order_date) between '2005-09-01' and '2005-09-30';
缺点:需提前知道每个月的最后一天
可用下面方法&