Conversion & Comparison, involving strings and dates in MySQL 5.6

我们有张表,表中有一个字段dpt_date,SQL类型为date type,表示离开日期。
我们将dpt_date与字符串‘2016-03-09’进行比较,发现效率低于dpt_date转换为字符串再与‘2016-03-09’进行比较:
SELECT * FROM tbl_name WHERE dpt_date = '2016-03-09' 效率低于 SELECT * FROM tbl_name WHERE FORMAT_DATE(dpt_date, '%Y-%m-%d')='2016-03-09'。
It can be inferred that conversion from string to date and comparison between dates is not as efficient as conversion from date to string and string comparison.
 
可以查看Type Conversion in Expression Evaluation中的相关部分:
 
The following rules describe how conversion occurs for comparison operations:
  • If one or both arguments are NULL, the result of the comparison is NULL, except for the NULL-safe <=> equality comparison operator. For NULL <=> NULL, the result is true. No conversion is needed.
  • If both arguments in a comparison operation are strings, they are compared as strings.
  • If both arguments are integers, they are compared as integers.
  • Hexadecimal values are treated as binary strings if not compared to a number.
  • If one of the arguments is a TIMESTAMP or DATETIME column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed. This is done to be more ODBC-friendly. This is not done for the arguments to IN(). To be safe, always use complete datetime, date, or time strings when doing comparisons. For example, to achieve best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.

    A single-row subquery from a table or tables is not considered a constant. For example, if a subquery returns an integer to be compared to a DATETIME value, the comparison is done as two integers. The integer is not converted to a temporal value. To compare the operands as DATETIME values, use CAST() to explicitly convert the subquery value to DATETIME.
  • If one of the arguments is a decimal value, comparison depends on the other argument. The arguments are compared as decimal values if the other argument is a decimal or integer value, or as floating-point values if the other argument is a floating-point value.
  • In all other cases, the arguments are compared as floating-point (real) numbers.
 
 
 
 
 

MySQL5.6中date和string的转换和比较的更多相关文章

  1. Java Date,long,String 日期转换

    1.java.util.Date类型转换成long类型java.util.Date dt = new Date();System.out.println(dt.toString()); //java. ...

  2. java中Date与String的相互转化

    1:大体思路 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new Date("2008-04 ...

  3. Java中Date、String、Calendar类型之间的转化

    1.Calendar 转化 String   //获取当前时间的具体情况,如年,月,日,week,date,分,秒等   Calendar calendat = Calendar.getInstanc ...

  4. iOS中date和string的相互转换

    必须知道的内容 G: 公元时代,例如AD公元     yy: 年的后2位     yyyy: 完整年     MM: 月,显示为1-12     MMM: 月,显示为英文月份简写,如 Jan      ...

  5. Date与String互相转换及日期的大小比较

    private static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; /** * String转Date * * @param ...

  6. mybatis与mysql中的Date和String之间转换

    在javaweb开发过程中,难免会使用日期类型,在持久化时java的日期格式可以是String,Date.mysql格式可以是varchar,datetime.他们之间如何进行相互转化? 1 java ...

  7. Java中int和String互相转换的多种方法

    1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  8. Java中Date与String的相互转换

    我们在注册网站的时候,往往需要填写个人信息,如姓名,年龄,出生日期等,在页面上的出生日期的值传递到后台的时候是一个字符串,而我们存入数据库的时候确需要一个日期类型,反过来,在页面上显示的时候,需要从数 ...

  9. 关于date和String互相转换的问题

    其实原理很简单,就是将String类型的变量使用SimpleDateFormat来转换成Date,然后用getTime()方法比较 SimpleDateFormat sdf = new SimpleD ...

随机推荐

  1. 使用JSCH框架通过跳转机访问其他节点

    之前搞了套远程访问ssh进行操作的代码,最近有需求,需要通过一台跳转机才能访问目标服务.在网上搜了半天,也没找到比较好的例子,就自己翻阅了下JSCH的API.但是看的云里雾里的.联想了下,端口转发的原 ...

  2. liunx分布式监控工具

    监控你的WEB服务器或者WEB主机运行是否正常与健康是非常重要的.你要确保用户始终可以打开你的网站并且网速不慢.服务器监控工具允许你收集和分析有关你的Web服务器的数据. 有许多非常好的服务器监控解决 ...

  3. mysql 有哪些索引

    Mysql支持哪几种索引 从数据结构角度 1.B+树索引(O(log(n))):关于B+树索引,可以参考 MySQL索引背后的数据结构及算法原理 2.hash索引:a 仅仅能满足"=&quo ...

  4. 微信原始demo

    有可能下载最新的demo下载不到这个原版的精简demo了. 这里为大家提供一下.以下代码为原始未改动代码   01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 ...

  5. centos7 简单搭建lnmp环境

    1:查看环境: 1 2 [root@10-4-14-168 html]# cat /etc/redhat-release CentOS release 6.5 (Final) 2:关掉防火墙 1 [r ...

  6. NodeJS爬虫入门

    1. 写在前面 往常都是利用 Python/.NET 语言实现爬虫,然现在作为一名前端开发人员,自然需要熟练 NodeJS.下面利用 NodeJS 语言实现一个糗事百科的爬虫.另外,本文使用的部分代码 ...

  7. SpringCloud Feign对Hystrix(断路由)的支持

    第一步:首先开启Feign对Hystrix的支持,在properties文件中添加以下配置: feign.hystrix.enabled=true. 第二步:在上一篇Feign的基础上添加Hystri ...

  8. 最短路算法之Dijkstra算法通俗解释

    Dijkstra算法 说明:求解从起点到任意点的最短距离,注意该算法应用于没有负边的图. 来,看图. 用邻接矩阵表示 int[][] m = { {0, 0, 0, 0, 0, 0}, {0, 0, ...

  9. nginx防恶意域名解析

    今天无意间查看访问日志发现一个fhxywh.com的域名居然解析到了我的服务器,也就是说通过这个域名也能访问我的博客,这个就是赤裸裸的恶意域名解析了. 这个危害非常大,不仅会影响用户,而且不利于SEO ...

  10. stanford-parser for C#

    在项目里用到C#对英文句子进行词性标注.比較成熟的英文词性标注软件是stanford-parser.它个C#版本号,也是借助于IKVM完毕JAVA-C#的转换.详细配置过程例如以下: 1.下载stan ...