相关文章推荐
彷徨的山楂  ·  斯坦福Machine Learning ...·  2 年前    · 

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)正确写法

sql_2=''' select * from tables1 where log_date>%s and log_date<%s and unit_id=%d

''' % ("'" + 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';

1、 a表字段值更新到b表某字段 update b set b.name = (select a.name from a where a.id = b.id) 2、判断a表某 日期 字段 大于 当前 日期 select * from a where a.date > curdate()
1、select 语句 select是 SQL 最常用的查询 语句 ,从某个表 查找相应的列,基本的使用方法是:select <列名>,<列名>......————————select子句from <表名>;—————————from子句例如:1.--从客户表 查询客户姓名和工资列 select 客户姓名,工资 from 客户表; 注:用...
关于 MySQL 日期 的使用1 日期 等于 \ 大于 \ 小于 2 日期 增加一个时间段 date_add()3 日期 减小一个时间段 date_sub()4 两个 日期 相差比较5 时间截取6 常见小案例 最近在使用 sql 函数时,因项目数据库不同,导致 sql 日期 的使用方法大不相同, 一些常见的 日期 计算,都有不同的应用,故整理记录一下 MySQL 日期 用法。 以user表为例 CREATE TABLE `user` ( `id` bigint(20) NOT NULL COMMENT '主键ID', `name` var
SELECT * from table where TIMESTAMPDIFF(type,pretime,latertime)>100 TIMESTAMPDIFF函数,需要三个参数,type是比较的类型,可以比较FRAC_SECOND、SECOND、 MINUTE、 HOUR、 DAY、 WEEK、 MONTH、 QUARTER或 YEAR几种类型,pretime是前一个时间,比较时用后一个时间减前一个时间` SELECT * from job_order where TIMESTAMPDI
使用 sql 语句 查询 日期 select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查询当天 日期 在一周 的数据 select * from ShopOrder where datediff(day,ordTime,getdate()-1)=0 //查询当天的所有数据 --查询当天: select * from info where DateDiff(dd,datetime,getdate())=0