当前位置:   article > 正文

MySQL cast()函数

mysql cast

转载自   MySQL cast()函数

MySQL CAST函数介绍

MySQL CAST()函数的语法如下:

CAST(expression AS TYPE);

CAST()函数将任何类型的值转换为具有指定类型的值。目标类型可以是以下类型之一:BINARYCHARDATEDATETIMETIMEDECIMALSIGNEDUNSIGNED

CAST()函数通常用于返回具有指定类型的值,以便在WHEREJOINHAVING子句中进行比较。

我们来看一下使用CAST()函数的一些例子。

MySQL CAST函数示例

在下面的例子中,在进行计算之前,MySQL将一个字符串隐式转换成一个整数:

  1. mysql> SELECT (1 + '1')/2;
  2. +-------------+
  3. | (1 + '1')/2 |
  4. +-------------+
  5. | 1 |
  6. +-------------+
  7. 1 row in set

要将字符串显式转换为整数,可以使用CAST()函数,如以下语句:

  1. mysql> SELECT (1 + CAST('1' AS UNSIGNED))/2;
  2. +-------------------------------+
  3. | (1 + CAST('1' AS UNSIGNED))/2 |
  4. +-------------------------------+
  5. | 1 |
  6. +-------------------------------+
  7. 1 row in set

以下语句明确地将整数转换为字符串,并将该字符串与另一个字符串连接:

  1. mysql> SELECT CONCAT('MySQL CAST example #',CAST(2 AS CHAR));
  2. +------------------------------------------------+
  3. | CONCAT('MySQL CAST example #',CAST(2 AS CHAR)) |
  4. +------------------------------------------------+
  5. | MySQL CAST example #2 |
  6. +------------------------------------------------+
  7. 1 row in set

我们来看看示例数据库(yiibaidb)中的orders表,其表结构如下 -

  1. mysql> desc orders;
  2. +----------------+-------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------------+-------------+------+-----+---------+-------+
  5. | orderNumber | int(11) | NO | PRI | NULL | |
  6. | orderDate | date | NO | | NULL | |
  7. | requiredDate | date | NO | | NULL | |
  8. | shippedDate | date | YES | | NULL | |
  9. | status | varchar(15) | NO | | NULL | |
  10. | comments | text | YES | | NULL | |
  11. | customerNumber | int(11) | NO | MUL | NULL | |
  12. +----------------+-------------+------+-----+---------+-------+
  13. 7 rows in set

请参阅以下查询:

  1. SELECT orderNumber,
  2. requiredDate
  3. FROM orders
  4. WHERE requiredDate BETWEEN '2013-01-01' AND '2013-01-31';

执行上面语句,得到以下结果 -

  1. +-------------+--------------+
  2. | orderNumber | requiredDate |
  3. +-------------+--------------+
  4. | 10100 | 2013-01-13 |
  5. | 10101 | 2013-01-18 |
  6. | 10102 | 2013-01-18 |
  7. +-------------+--------------+
  8. 3 rows in set

查询选择要求日期(requiredDate)在2013年1月的订单。requireDate列的数据类型为DATE,因此MySQL必须将文字字符串“2013-01-01”“2013-01-31”在评估WHERE条件之前转换为TIMESTAMP值 。

但是,为了安全起见,可以使用CAST()函数将字符串显式转换为TIMESTAMP值,如下所示:

  1. SELECT orderNumber,
  2. requiredDate
  3. FROM orders
  4. WHERE requiredDate BETWEEN CAST('2013-01-01' AS DATETIME)
  5. AND CAST('2013-01-31' AS DATETIME);

执行上面查询语句,得到以下结果 -

  1. +-------------+--------------+
  2. | orderNumber | requiredDate |
  3. +-------------+--------------+
  4. | 10100 | 2013-01-13 |
  5. | 10101 | 2013-01-18 |
  6. | 10102 | 2013-01-18 |
  7. +-------------+--------------+
  8. 3 rows in set

以下语句将DOUBLE值转换为CHAR值,并将结果用作CONCAT函数的参数:

  1. SELECT productName,
  2. CONCAT('Prices(',
  3. CAST(buyprice AS CHAR),
  4. ',',
  5. CAST(msrp AS CHAR),
  6. ')') prices
  7. FROM products;

执行上面查询语句,得到以下结果 -

  1. +---------------------------------------------+-----------------------+
  2. | productName | prices |
  3. +---------------------------------------------+-----------------------+
  4. | 1969 Harley Davidson Ultimate Chopper | Prices(48.81,95.30) |
  5. | 1952 Alpine Renault 1300 | Prices(98.58,214.30) |
  6. | 1996 Moto Guzzi 1100i | Prices(68.99,118.94) |
  7. | 2003 Harley-Davidson Eagle Drag Bike | Prices(91.02,193.66) |
  8. | 1972 Alfa Romeo GTA | Prices(85.68,136.00) |
  9. ***************** 此处省略了一大波数据 *******************************************
  10. | 1982 Camaro Z28 | Prices(46.53,101.15) |
  11. | ATA: B757-300 | Prices(59.33,118.65) |
  12. | F/A 18 Hornet 1/72 | Prices(54.40,80.00) |
  13. | The Titanic | Prices(51.09,100.17) |
  14. | The Queen Mary | Prices(53.63,99.31) |
  15. | American Airlines: MD-11S | Prices(36.27,74.03) |
  16. | Boeing X-32A JSF | Prices(32.77,49.66) |
  17. | Pont Yacht | Prices(33.30,54.60) |
  18. +---------------------------------------------+-----------------------+
  19. 110 rows in set

在本教程中,您学习了如何使用MySQL CAST()函数将任何类型的值转换成指定类型的值。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号