Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL将高度格式转换为厘米_Mysql_Sql - Fatal编程技术网

MySQL将高度格式转换为厘米

MySQL将高度格式转换为厘米,mysql,sql,Mysql,Sql,我想把英尺和英寸转换成厘米格式 my DB中的格式为: 4英尺6英寸(4英尺6英寸) 厘米换算公式 4*30.48 = 121.92 (convert feet to centimeters = multiply by 30.48) 6*2.54 = 15.24 (convert inches to centimeters = multiply by 2.54) So Result = 121.92 + 15.24 = 137.16 cm 例如: 实际表格:英寸 SELECT * FROM

我想把英尺和英寸转换成厘米格式

my DB中的格式为:

4英尺6英寸(4英尺6英寸)

厘米换算公式

4*30.48 = 121.92 (convert feet to centimeters = multiply by 30.48)
6*2.54 = 15.24 (convert inches to centimeters   = multiply by 2.54)
So Result = 121.92 + 15.24 = 137.16 cm
例如:

实际表格:英寸

SELECT * FROM inches

id height
1  4'6"
2  4'7"
3  5'8"
4  5'9"
在执行SQL查询时,我希望以下结果为厘米

id height
1  137.16
2  139.7
3  172.72
4  175.26

提前感谢:)

在应用程序级别上可能要容易得多,但如果确实需要,您可以在SQL中这样做,使用and函数和一些基本数学:

SET @height = '4''6"';
SELECT
    SUBSTR(@height, 1, INSTR(@height, '''') - 1) * 12 * 2.54 +
    SUBSTR(@height, INSTR(@height, '''') + 1, INSTR(@height, '"') - INSTR(@height, '''') - 1) * 2.54;

-- yields 137.16
或者,应用于表结构:

SELECT id,
    SUBSTR(height, 1, INSTR(height, '''') - 1) * 12 * 2.54 +
    SUBSTR(height, INSTR(height, '''') + 1, INSTR(height, '"') - INSTR(height, '''') - 1) * 2.54 AS height
FROM inches;

应用程序端流程会更好

但是,

    SELECT 
            (CAST(SUBSTR(height,1, LOCATE("'",height)-1) AS UNSIGNED) * 30.48) + 
            (CAST(SUBSTR(height,   LOCATE("'",height)+1) AS UNSIGNED) * 2.54 )   AS cm
    FROM 
            inches;

你为什么不先把它换算成英寸,然后再乘以2.54呢?也就是说,
(4 x 12+6)x 2.54
?是的,很好。如何进行查询?请…类似的问题:。简而言之,您应该在应用程序级别进行查询,例如通过PHP脚本。@Raptor,谢谢您的建议,但我确实需要在SQL中进行查询side@Robby,不,不会的,我检查了两次,请在你身边试试!@Asik,不客气!不过我还是更喜欢要在应用程序端执行此操作!无论如何,选择/首选项/要求始终是您的!@Raptor,您有道理!是的,只有当值小于0'2“>