我们有张表,表中有一个字段 dpt_date ,SQL 类型为 date,表示离开日期。
我们将 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'。

字符串的比较比日期更高效。

 
可以查看 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.
 
 
 
 
 

MySQL 5.6 date 与 string 的转换和比较的更多相关文章

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

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

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

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

  3. MySQL5.6中date和string的转换和比较

    Conversion & Comparison, involving strings and dates in MySQL 5.6 我们有张表,表中有一个字段dpt_date,SQL类型为da ...

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

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

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

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

  6. 【MySQL】【3】String和Date相互转换

    正文: 1,Date转String --结果:<2019-04-10> SELECT DATE_FORMAT(SYSDATE(), "%Y-%m-%d") FROM D ...

  7. Java日期与时间的处理/Date,String,Calendar转换

    public class Demo01 { //Java中Date类和Calendar简介 public static void main(String[] args) { long now=Syst ...

  8. java Date和String转换总结

    java.util.Date和String类型的转换是非常常用的,现在总结一下: 1. Date转换为String //Date --->String DateFormat dft = new ...

  9. java Date时间的各种转换方式和Mysql存时间类型字段的分析

    一:各种Date之间的转换方法 public class TimeTest { public static void main(String[] args) { Date date = new Dat ...

随机推荐

  1. leetcode刷题2:两数相加add_two_numbers

    题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...

  2. JAVA Excel导入导出

    --------------------------------------------方式一(新)-------------------------------------------------- ...

  3. LeetCode——sum-root-to-leaf-numbers

    Question Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a ...

  4. InnoDB并发事务

    ​目录 1.行锁:索引加锁 2.意向锁 3.间隙锁 4.MVCC机制 行锁 InnoDB通过多版本并发控制MVCC来支持事务 InnoDB的设计是为了在处理大数据量的时候得到最好的性能.InnoDB存 ...

  5. mac中的echo颜色输出

    mac: echo "\033[1;36mSister Lin Fall from the Sky\033[0m" ubuntu: echo -e "\e[1;36mSi ...

  6. Mac开机启动

    1. Finder打开资源库的LaunchAgents目录. 打开Finder,按⇧⌘G,输入 /Library/LaunchAgents/ 以及 ~/Library/LaunchAgents/ 2. ...

  7. Python学习之路day3-函数

    一.函数基础 编程方法典型的编程方法有面向过程.面向对象和函数式编程.面向过程是把编程的重点放在实现过程上,分析出结局问题所需的步骤过程,然后通过语句来一一定义实现.面向对象是把构成问题的事务分界成若 ...

  8. 17-THREE.JS 光晕滤镜

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  9. Linux_总结_02_最小化安装后需要安装和更新的命令

    一.前言 二.安装命令 1.配置yum源 2.更新yum sudo yum -y update 3.安装ifconfig 最小化安装后,是无法使用ifconfig命令的. 可参见:CentOS7下解决 ...

  10. MAMP mac下启动Mysql

    MAMP mac下启动Mysql /Applications/MAMP/Library/bin/mysql -u root -p 初始密码 root: