计算速度SQL
我有一个表,列为“时间和列距离” 我想有一个按小时计算速度的专栏 这是我的桌子计算速度SQL,sql,Sql,我有一个表,列为“时间和列距离” 我想有一个按小时计算速度的专栏 这是我的桌子 +---------+---------+----------------------+----------+--------------------+ | comp | idname | name | distance | time | +---------+---------+----------------------+----------+
+---------+---------+----------------------+----------+--------------------+
| comp | idname | name | distance | time |
+---------+---------+----------------------+----------+--------------------+
| 1041321 | 1230274 | Berlin Calling (IRE) | 1400 | 1'30"74 |
| 1086603 | 1230274 | Berlin Calling (IRE) | 1600 | 1'42"05 |
| 1106577 | 1230274 | Berlin Calling (IRE) | 1650 | 1'46"56 |
| 1009518 | 1230274 | Berlin Calling (IRE) | 1700 | 1'44"23 |
| 1166930 | 1230274 | Berlin Calling (IRE) | 1800 | 2'06"54 |
+---------+---------+----------------------+----------+--------------------+
我想要这样的输出:
+---------+---------+----------------------+----------+--------------------+------------+
| comp | idname | name | distance | time | speed/KM/h |
+---------+---------+----------------------+----------+--------------------+------------+
| 1041321 | 1230274 | Berlin Calling (IRE) | 1400 | 1'30"74 | 55,5 |
| 1086603 | 1230274 | Berlin Calling (IRE) | 1600 | 1'42"05 | 56,4 |
| 1106577 | 1230274 | Berlin Calling (IRE) | 1650 | 1'46"56 | 55,7 |
| 1009518 | 1230274 | Berlin Calling (IRE) | 1700 | 1'44"23 | 58,7 |
| 1166930 | 1230274 | Berlin Calling (IRE) | 1800 | 2'06"54 | 51,13 |
+---------+---------+----------------------+----------+--------------------+------------+
感谢您的帮助日期/时间和字符串解析函数特定于数据库。基于您提出的其他问题,我假设您正在使用MySQL 如果是,您可以使用:
select t.*,
distance / (substring_index(time, '''', 1) * 24.0 +
substring_index(substring_index(time, '''', 2), '"') * 1.0 +
substring_index(time, '"', -1) / (60.0)
) as speed
from t;
其他数据库具有解析字符串的功能,但这在不同的数据库中差异很大
我强烈建议您将时间存储为十进制小时,而不是使用晦涩难懂的格式。请用您正在使用的数据库标记您的问题。通常最好使用
间隔
数据类型或INT
(秒)来存储时间,而不是VARCHAR
VARCHAR
容易出错且难以管理。时间列位于:MEDIUMTEXT中