mysql中大于等于某个年月,小于等于当前年月之间的所有年月的语句如何写呢
mysql中小于某个日期,大于某个日期,或者大于等于某个年月,小于等于当前年月之间的所有年月的语句如何写呢
mysql日期查询写法示例如下:
1.查询大于等于一个开始日期,小于等于一个结束日期的正确写法,
1)第一种正确写法:
sql_1 = """
select * from tables1 where log_date between %s and %s and unit_id=%d
""" % (""" + start_day + """, """ + end_day + """,unit_id)
结论:sql_1是正确的,返回的包含start_day和end_day,以及start_day到end_day之间的数据
2)第二种正确写法:
mysql> select * from tables1 where log_date >= "2017-06-25" and log_date <= "2017-07-01";
对比,以下是错误方法:
mysql> select * from tables1 where log_date >= "2017-06-25" and log_date =< "2017-07-01";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "=< "2017-07-01"" at line 1
3)正确示例3
select date from table where date between "2013-05" and "2014-5"
返回的包含2013年5月和2014年5月,以及2014年5月到2014年5月之间的数据
3.查询大于一个开始日期,小于一个结束日期的写法:
1)正确写法
""" % (""" + start_day + """, """ + end_day + """,unit_id)
结论:sql_2是正确的,返回的不包含start_day和end_day,仅仅包含start_day到end_day之间的数据
2)错误写法
sql_3=""" select * from tables1 where log_date>=%s and log_date=<%s and unit_id=%d
""" % (""" + start_day + """, """ + end_day + """,unit_id)
结论:sql_3是错误的写法
3. 大于等于一个日期的写法
mysql> select * from tables1 where log_date >= "2017-07-15";
4. 小于等于一个日期的写法
mysql> select * from tables1 where log_date <="2017-06-27";
- 上一篇: 如何使用python将大量数据导出到Excel中的小技巧
- 下一篇:没有了