MySQL cast()函数
轉(zhuǎn)載自? ?MySQL cast()函數(shù)
MySQL CAST函數(shù)介紹
MySQL?CAST()函數(shù)的語法如下:
CAST(expression AS TYPE);CAST()函數(shù)將任何類型的值轉(zhuǎn)換為具有指定類型的值。目標(biāo)類型可以是以下類型之一:BINARY,CHAR,DATE,DATETIME,TIME,DECIMAL,SIGNED,UNSIGNED。
CAST()函數(shù)通常用于返回具有指定類型的值,以便在WHERE,JOIN和HAVING子句中進(jìn)行比較。
我們來看一下使用CAST()函數(shù)的一些例子。
MySQL CAST函數(shù)示例
在下面的例子中,在進(jìn)行計算之前,MySQL將一個字符串隱式轉(zhuǎn)換成一個整數(shù):
mysql> SELECT (1 + '1')/2; +-------------+ | (1 + '1')/2 | +-------------+ | 1 | +-------------+ 1 row in set要將字符串顯式轉(zhuǎn)換為整數(shù),可以使用CAST()函數(shù),如以下語句:
mysql> SELECT (1 + CAST('1' AS UNSIGNED))/2; +-------------------------------+ | (1 + CAST('1' AS UNSIGNED))/2 | +-------------------------------+ | 1 | +-------------------------------+ 1 row in set以下語句明確地將整數(shù)轉(zhuǎn)換為字符串,并將該字符串與另一個字符串連接:
mysql> SELECT CONCAT('MySQL CAST example #',CAST(2 AS CHAR)); +------------------------------------------------+ | CONCAT('MySQL CAST example #',CAST(2 AS CHAR)) | +------------------------------------------------+ | MySQL CAST example #2 | +------------------------------------------------+ 1 row in set我們來看看示例數(shù)據(jù)庫(yiibaidb)中的orders表,其表結(jié)構(gòu)如下 -
mysql> desc orders; +----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------+------+-----+---------+-------+ | orderNumber | int(11) | NO | PRI | NULL | | | orderDate | date | NO | | NULL | | | requiredDate | date | NO | | NULL | | | shippedDate | date | YES | | NULL | | | status | varchar(15) | NO | | NULL | | | comments | text | YES | | NULL | | | customerNumber | int(11) | NO | MUL | NULL | | +----------------+-------------+------+-----+---------+-------+ 7 rows in set請參閱以下查詢:
SELECT orderNumber,requiredDate FROM orders WHERE requiredDate BETWEEN '2013-01-01' AND '2013-01-31';執(zhí)行上面語句,得到以下結(jié)果 -
+-------------+--------------+ | orderNumber | requiredDate | +-------------+--------------+ | 10100 | 2013-01-13 | | 10101 | 2013-01-18 | | 10102 | 2013-01-18 | +-------------+--------------+ 3 rows in set查詢選擇要求日期(requiredDate)在2013年1月的訂單。requireDate列的數(shù)據(jù)類型為DATE,因此MySQL必須將文字字符串“2013-01-01”和“2013-01-31”在評估WHERE條件之前轉(zhuǎn)換為TIMESTAMP值 。
但是,為了安全起見,可以使用CAST()函數(shù)將字符串顯式轉(zhuǎn)換為TIMESTAMP值,如下所示:
SELECT orderNumber,requiredDate FROM orders WHERE requiredDate BETWEEN CAST('2013-01-01' AS DATETIME)AND CAST('2013-01-31' AS DATETIME);執(zhí)行上面查詢語句,得到以下結(jié)果 -
+-------------+--------------+ | orderNumber | requiredDate | +-------------+--------------+ | 10100 | 2013-01-13 | | 10101 | 2013-01-18 | | 10102 | 2013-01-18 | +-------------+--------------+ 3 rows in set以下語句將DOUBLE值轉(zhuǎn)換為CHAR值,并將結(jié)果用作CONCAT函數(shù)的參數(shù):
SELECT productName,CONCAT('Prices(',CAST(buyprice AS CHAR),',',CAST(msrp AS CHAR),')') prices FROM products;執(zhí)行上面查詢語句,得到以下結(jié)果 -
+---------------------------------------------+-----------------------+ | productName | prices | +---------------------------------------------+-----------------------+ | 1969 Harley Davidson Ultimate Chopper | Prices(48.81,95.30) | | 1952 Alpine Renault 1300 | Prices(98.58,214.30) | | 1996 Moto Guzzi 1100i | Prices(68.99,118.94) | | 2003 Harley-Davidson Eagle Drag Bike | Prices(91.02,193.66) | | 1972 Alfa Romeo GTA | Prices(85.68,136.00) | ***************** 此處省略了一大波數(shù)據(jù) ******************************************* | 1982 Camaro Z28 | Prices(46.53,101.15) | | ATA: B757-300 | Prices(59.33,118.65) | | F/A 18 Hornet 1/72 | Prices(54.40,80.00) | | The Titanic | Prices(51.09,100.17) | | The Queen Mary | Prices(53.63,99.31) | | American Airlines: MD-11S | Prices(36.27,74.03) | | Boeing X-32A JSF | Prices(32.77,49.66) | | Pont Yacht | Prices(33.30,54.60) | +---------------------------------------------+-----------------------+ 110 rows in set在本教程中,您學(xué)習(xí)了如何使用MySQL?CAST()函數(shù)將任何類型的值轉(zhuǎn)換成指定類型的值。
總結(jié)
以上是生活随笔為你收集整理的MySQL cast()函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL last_insert_id
- 下一篇: 从域名到电子商务,BDWEB IT都能做