Home > Database > Mysql Tutorial > Mysql 字段类型转化 和 时间类型相关处理

Mysql 字段类型转化 和 时间类型相关处理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:41:00
Original
1275 people have browsed it

I) 字段类型转化 MySQL 的CAST()和CONVERT()函数可用来获取一个类型的,并产生另一个类型的。两者具体的语法如下: CAST(value as type);CONVERT(value, type); 就是CAST(xxx AS 类型), CONVERT(xxx,类型)。 可以转换的类型是有限制的。这个类型可以是以下其

I) 字段类型转化

MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值。两者具体的语法如下:

CAST(value as type);
CONVERT(value, type);
Copy after login

就是CAST(xxx AS 类型), CONVERT(xxx,类型)。

可以转换的类型是有限制的。这个类型可以是以下值其中的一个:

  • 二进制,同带binary前缀的效果 : BINARY    
  • 字符型,可带参数 : CHAR()     
  • 日期 : DATE     
  • 时间: TIME     
  • 日期时间型 : DATETIME     
  • 浮点数 : DECIMAL
  • 整数 : SIGNED     
  • 无符号整数 : UNSIGNED
EEG:
mysql> SELECT CAST('3.35' AS signed);
+------------------------+
| CAST('3.35' AS signed) |
+------------------------+
|                      3 |
+------------------------+
1 row in set
<pre class="brush:php;toolbar:false">mysql> SELECT CONVERT('23.001',SIGNED);
+----------------------+
| CONVERT('23',SIGNED) |
+----------------------+
|                   23 |
+----------------------+
1 row in set
Copy after login

转化为float:

 SELECT  23+0.000

输出:23.000

以下为项目中应用的SQL语句实例:

/*SQLServerDB query methodconvert to float
   return this.getSession().createQuery(" FROM "+this.clazz.getName()+" this WHERE this.areaCode IN(:areaCode) and " +
               "(convert(float,this.cpuUsedPer) > '"+Float.valueOf(cpuValue)+"' or convert(float,this.memoryUsedPer) > '"+Float.valueOf(memValue)+"' or    

                convert(float,this.diskLaveSize)

                convert(float,DateDiff(mi,this.lastActiveTime,getDate()))> '"+Float.parseFloat(timeOutValue)+"')") .setParameterList("areaCode", areaCode) .list();
 */

  //@author:chenjun /20140515 MysqlDB query method with convert DECIMAL
   return this.getSession().createQuery(" FROM "+this.clazz.getName()+" this WHERE this.areaCode IN(:areaCode) and " +
              "(this.cpuUsedPer+0.00) > '"+Float.valueOf(cpuValue)+"' or  (this.memoryUsedPer+0.00) > '"+Float.valueOf(memValue)+"' or  (this.diskLaveSize+0.00)

              '"+Float.valueOf(diskValue)+ "' or this.appServerState=1  or this.mgrServerState=1 or convert(TIMESTAMPDIFF(MINUTE,this.lastActiveTime,NOW()),DECIMAL)> 

              '"+Float.parseFloat(timeOutValue)+"')").setParameterList("areaCode", areaCode) .list();


II )时间类型相关处理

   SQLServer

  DateDiff(mi,this.lastActiveTime,getDate())  返回lastActiveTime Date 时间与当前系统时间的时间差,单位为分钟


   Mysql

<span> DATEDIFF(expr,expr2) 
DATEDIFF() 返回起始时间 expr和结束时间expr2之间<span>的天数</span>
</span><pre class="brush:php;toolbar:false"><span> expr > expr2 ,返回值 > 0 ;</span><span> expr  = expr2 ,返回值= 0 ;</span><span>expr </span>
Copy after login
<span> TIMEDIFF(expr,expr2) 
TIMEDIFF() 返回起始时间 expr 和结束时间expr2 之间的时间</span>
Copy after login
<pre class="brush:php;toolbar:false"><span> expr > expr2 ,返回值 > 0 ;</span><span> expr  = expr2 ,返回值= 0 ;</span><span>expr </span>
Copy after login
 TIMESTAMPDIFF(MINUTE,expr,expr2) TIMEDIFF() 返回起始时间 expr 和结束时间expr2 之间的分钟数
<pre class="brush:php;toolbar:false"><pre class="brush:php;toolbar:false"><span>  expr > expr2 ,返回值 <span> expr  = expr2 ,返回值= 0 ;</span><span>expr > expr2 ,返回值<span>DATEDIFF</span>、<span>TIMEDIFF</span>相反)</span></span>
Copy after login

EGG:     TIMESTAMPDIFF(MINUTE,expr,NOW()) 
<span>测试值:expr</span>=‘2015-05-15 17:00:10’   ;NOW() = ‘2015-05-15 17:40:10’,输出:40

<pre class="brush:php;toolbar:false">mysql> <strong><code>SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');</code></strong>
        -> 3
mysql> <strong><code>SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');</code></strong>
        -> -1
mysql> <strong><code>SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');</code></strong>
        -> 128885
Copy after login

MySQL - DATEDIFF(expr1,expr2)

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff

MySQL语法

CREATE TABLE,http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#create-table

SHOW TABLE STATUS,http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#show-table-status

日期和时间类型概述文档

http://dev.mysql.com/doc/refman/5.1/zh/column-types.html#date-and-time-type-overview

JOIN语法文档,

http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#join




source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
Mysql cannot start
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template