我有一个表格,其中时间长度类型存储为VARCHAR,并尝试选择所有超过2个半小时的时间:
mysql> SELECT * FROM `1970’s Movies`.movies WHERE length;
+----------------------------------+------+---------------+-------------+------------------+
| title | year | 5-star rating | length | studio |
+----------------------------------+------+---------------+-------------+------------------+
| Star Wars | 1977 | 5 | 2 hr 1 min | 20th Century Fox |
| National Lampoon’s Animal House | 1978 | 3 | 1 hr 49 min | Universal |
| Jaws | 1975 | 4 | 2 hr 5 min | Universal |
| The Exorcist | 1973 | 4 | 2 hr 2 min | Warner Bros. |
| Grease | 1978 | 2 | 1 hr 50 min | Paramount |
| The Godfather | 1972 | 5 | 2 hr 55 min | Paramount |
| The Towering Inferno | 1974 | 3 | 2 hr 45 min | 20th Century Fox |
| The Black Hole | 1979 | 1 | 1 hr 38 min | Disney |
| Superman | 1978 | 2 | 2 hr 23 min | Warner Bros. |
| The Godfather Part II | 1974 | 5 | 3 hr 20 min | Paramount |
| Love Story | 1970 | 1 | 1 hr 40 min | Paramount |
+----------------------------------+------+---------------+-------------+------------------+
答案 0 :(得分:0)
将数字转换为字符串时,MySQL是允许的,所以你可以让它适合你。例如:
SELECT CONVERT('2 hr 1 min', UNSIGNED) --> returns 2
那会给你时间。要获得会议记录,请在' hr'之后查看三个字符:
SELECT SUBSTR(length, LOCATE('hr', length) + 3) --> returns '1 min' for Star Wars
如果您将转换应用于' 1分钟',MySQL将返回数字1。
要以小时和分钟显示电影标题,请执行以下操作:
SELECT
title,
CONVERT(length, UNSIGNED) AS hours,
CONVERT(SUBSTR(length, LOCATE('hr', length) + 3), UNSIGNED) as minutes
FROM movies
仅播放超过两个半小时的电影:
SELECT *
FROM movies
WHERE CONVERT(length, UNSIGNED) >= 2
AND CONVERT(SUBSTR(length, LOCATE('hr', length) + 3), UNSIGNED) >= 30
答案 1 :(得分:0)
"对"答案是将length
转换为一个简单的数字,可能以"分钟"为单位。 之前存储到数据库中。然后在阅读时将其格式化。
SMALLINT UNSIGNED
绰绰有余;它可以处理大约1000个小时。
想象一下,如果您选择将第一个显示为' 2:01'而不是< 2小时1分钟'?
2小时' 2小时'与' 2小时0分钟'?