Pesquisa de site

Arredondar segundos para meio minuto mais próximo no MySQL?


Para arredondar os segundos para o meio minuto mais próximo, use CEILING(). Vamos primeiro criar uma tabela:

mysql> create table DemoTable (secondValue int);
Query OK, 0 rows affected (0.64 sec)

Exemplo

Insira alguns registros na tabela usando o comando insert:

mysql> insert into DemoTable values(27);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values(56);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(118);
Query OK, 1 row affected (0.20 sec)

Exiba todos os registros da tabela usando a instrução select:

mysql> select *from DemoTable;

Saída

+-------------+
| secondValue |
+-------------+
| 27          |
| 56          |
| 118         |
+-------------+
3 rows in set (0.00 sec)

A seguir está a consulta para arredondar segundos:

mysql> select secondValue, CEILING(secondValue / 30) / 2 AS ApproxMinutes from DemoTable;

Saída

+-------------+---------------+
| secondValue | ApproxMinutes |
+-------------+---------------+
| 27          | 0.5000        |
| 56          | 1.0000        |
| 118         | 2.0000        |
+-------------+---------------+
3 rows in set (0.00 sec)

Artigos relacionados: