將高度格式轉換為釐米的 MySQL 查詢?
為此,請在 MySQL 中使用 CAST() 方法。我們首先建立一個表 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentHeight varchar(40) ) ; Query OK, 0 rows affected (0.47 sec)
使用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable(StudentHeight) values('5\'10\"');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(StudentHeight) values('4\'6\"');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(StudentHeight) values('5\'8\"');
Query OK, 1 row affected (0.10 sec)使用 select 語句顯示錶中的所有記錄 -
mysql> select *from DemoTable;
這將生成以下輸出 -
+----+---------------+ | Id | StudentHeight | +----+---------------+ | 1 | 5'10" | | 2 | 4'6" | | 3 | 5'8" | +----+---------------+ 3 rows in set (0.00 sec)
以下是將高度格式轉換為釐米的查詢 -
mysql> select
(cast(substr(StudentHeight,1,locate("'",StudentHeight)-1) as unsigned)*30.48)+
(cast(substr(StudentHeight,locate("'",StudentHeight)+1) as unsigned)*2.54) AS Centimeters
from DemoTable;這將生成以下輸出 -
+-------------+ | Centimeters | +-------------+ | 177.80 | | 137.16 | | 172.72 | +-------------+ 3 rows in set, 3 warnings (0.05 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP