MySQL cast()函式

2019-10-16 22:55:29

在本教學中,我們將向您展示如何使用MySQL CAST函式將任何型別的值轉換為具有指定型別的值。

MySQL CAST函式介紹

MySQL CAST()函式的語法如下:

CAST(expression AS TYPE);

CAST()函式將任何型別的值轉換為具有指定型別的值。目標型別可以是以下型別之一:BINARYCHARDATEDATETIMETIMEDECIMALSIGNEDUNSIGNED

CAST()函式通常用於返回具有指定型別的值,以便在WHEREJOINHAVING子句中進行比較。

我們來看一下使用CAST()函式的一些例子。

MySQL CAST函式範例

在下面的例子中,在進行計算之前,MySQL將一個字串隱式轉換成一個整數:

mysql> SELECT (1 + '1')/2;
+-------------+
| (1 + '1')/2 |
+-------------+
|           1 |
+-------------+
1 row in set

要將字串顯式轉換為整數,可以使用CAST()函式,如以下語句:

mysql> SELECT (1 + CAST('1' AS UNSIGNED))/2;
+-------------------------------+
| (1 + CAST('1' AS UNSIGNED))/2 |
+-------------------------------+
| 1                             |
+-------------------------------+
1 row in set

以下語句明確地將整數轉換為字串,並將該字串與另一個字串連線:

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

我們來看看範例資料庫(yiibaidb)中的orders表,其表結構如下 -

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';

執行上面語句,得到以下結果 -

+-------------+--------------+
| orderNumber | requiredDate |
+-------------+--------------+
|       10100 | 2013-01-13   |
|       10101 | 2013-01-18   |
|       10102 | 2013-01-18   |
+-------------+--------------+
3 rows in set

查詢選擇要求日期(requiredDate)在2013年1月的訂單。requireDate列的資料型別為DATE,因此MySQL必須將文字字串「2013-01-01」「2013-01-31」在評估WHERE條件之前轉換為TIMESTAMP值 。

但是,為了安全起見,可以使用CAST()函式將字串顯式轉換為TIMESTAMP值,如下所示:

SELECT orderNumber,
       requiredDate
FROM orders
WHERE requiredDate BETWEEN  CAST('2013-01-01' AS DATETIME)
                        AND CAST('2013-01-31' AS DATETIME);

執行上面查詢語句,得到以下結果 -

+-------------+--------------+
| orderNumber | requiredDate |
+-------------+--------------+
|       10100 | 2013-01-13   |
|       10101 | 2013-01-18   |
|       10102 | 2013-01-18   |
+-------------+--------------+
3 rows in set

以下語句將DOUBLE值轉換為CHAR值,並將結果用作CONCAT函式的引數:

SELECT productName,
       CONCAT('Prices(',
               CAST(buyprice AS CHAR),
               ',',
                CAST(msrp AS CHAR),
      ')') prices
FROM products;

執行上面查詢語句,得到以下結果 -

+---------------------------------------------+-----------------------+
| 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)  |
***************** 此處省略了一大波資料 *******************************************
| 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

在本教學中,您學習了如何使用MySQL CAST()函式將任何型別的值轉換成指定型別的值。