在我的数据库中格式是:以厘米为单位,大约137厘米。
我想把像137cm这样的例子转换成4'6“(4英尺6英寸)
实际表:以厘米为单位的高度
例:
SELECT * FROM height
id height_cm
1 137
2 139
3 172
4 175当我执行SQL查询时,我期望得到以下结果。
id height_finc
1 4'6"
2 4'7"
3 5'8"
4 5'9"公式是:1 inch = 2.54 cm和1 foot = 12 inches
发布于 2015-09-16 10:54:30
在执行select时,您需要对此执行一些算术操作。
让我们开始获取inch和foot
mysql> select id,
floor(height_cm/(12*2.54)) as foot ,
round((height_cm mod (12*2.54))/2.54) as inch from height ;
+------+------+------+
| id | foot | inch |
+------+------+------+
| 1 | 4 | 6 |
| 2 | 4 | 7 |
| 3 | 5 | 8 |
| 4 | 5 | 9 |
+------+------+------+现在使用concat,我们可以格式化显示
mysql> select id,
concat(
floor(height_cm/(12*2.54))
,'\''
,round((height_cm mod (12*2.54))/2.54)
,'"'
) as height_finc from height ;
+------+-------------+
| id | height_finc |
+------+-------------+
| 1 | 4'6" |
| 2 | 4'7" |
| 3 | 5'8" |
| 4 | 5'9" |
+------+-------------+发布于 2015-09-16 10:51:00
这基本上是算术和字符串格式:
select floor(height_cm / (2.54 * 12)) as feet,
(height_cm - floor(height_cm / (2.54 * 12)) * 12 * 2.54) / 2.54 as inches若要将格式设置为字符串:
select concat(floor(height_cm / (2.54 * 12)), '''',
round((height_cm - floor(height_cm / (2.54 * 12)) * 12 * 2.54) / 2.54), '"') as feet_inches我认为这很简单,因为:
select concat(floor(height_cm / (2.54 * 12)), '''',
round(height_cm / 2.54) % 12, '"')https://stackoverflow.com/questions/32606301
复制相似问题