How to understand minus operator between TIMESTAMP - mysql

mysql> select timestamp('2017-06-21 23:45:01') - timestamp('2017-06-21 23:44:58');
+---------------------------------------------------------------------+
| timestamp('2017-06-21 23:45:01') - timestamp('2017-06-21 23:44:58') |
+---------------------------------------------------------------------+
| 43 |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select version();
+------------+
| version() |
+------------+
| 5.7.16-log |
+------------+
1 row in set (0.00 sec)
Obviously, it is not calculated as timestamp. Please advise.

You have to use TIMEDIFF() or TIMESTAMPDIFF()
DEMO
SELECT TIMEDIFF(timestamp('2017-06-21 23:45:00') , timestamp('2017-06-21 23:44:58'));
-- result: 00:00:02.
SELECT TIMESTAMPDIFF(SECOND,'2017-06-21 23:45:00','2017-06-21 23:44:58');
-- result: -2

Related

Convert epoch time to gmt using mysql

I am running into trouble converting a time to utc/gmt.
select from_unixtime(1623167869);
2021-06-08 11:57:49
GMT should return 2021-06-08 14:57:49.
FROM_UNIXTIME
The return value is expressed in the session time zone.
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-unixtime
> SET time_zone = '+00:00';SELECT ##SESSION.time_zone;select from_unixtime(1623167869);
Query OK, 0 rows affected (0.000 sec)
+---------------------+
| ##SESSION.time_zone |
+---------------------+
| +00:00 |
+---------------------+
1 row in set (0.000 sec)
+---------------------------+
| from_unixtime(1623167869) |
+---------------------------+
| 2021-06-08 15:57:49 |
+---------------------------+
1 row in set (0.000 sec)
> SET time_zone = '-02:00';SELECT ##SESSION.time_zone;select from_unixtime(1623167869);
Query OK, 0 rows affected (0.000 sec)
+---------------------+
| ##SESSION.time_zone |
+---------------------+
| -02:00 |
+---------------------+
1 row in set (0.000 sec)
+---------------------------+
| from_unixtime(1623167869) |
+---------------------------+
| 2021-06-08 13:57:49 |
+---------------------------+
1 row in set (0.000 sec)

MySQL MIN & MAX on alpha numeric column

Simple MAX query:
SELECT MAX(Group) FROM acme
Works fine on a numeric column.
Is it possible to use to this on an alpha-numeric column and ignore anything other than a number?
yes if you cast it before use like this
mysql> SELECT MAX(CAST("123" AS UNSIGNED));
+------------------------------+
| MAX(CAST("123" AS UNSIGNED)) |
+------------------------------+
| 123 |
+------------------------------+
1 row in set (0.00 sec)
mysql> SELECT MAX(CAST("abc" AS UNSIGNED));
+------------------------------+
| MAX(CAST("abc" AS UNSIGNED)) |
+------------------------------+
| 0 |
+------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT MAX(CAST("-123" AS SIGNED));
+-----------------------------+
| MAX(CAST("-123" AS SIGNED)) |
+-----------------------------+
| -123 |
+-----------------------------+
1 row in set (0.00 sec)
replace your string column name with "abc" and modify query acording to your requirment

MySQL LIKE vs LOCATE

Does anyone know which one is faster:
SELECT * FROM table WHERE column LIKE '%text%';
or
SELECT * FROM table WHERE LOCATE('text',column)>0;
Added April 20th, 2015: Please read also Hallie's answer below
First one but marginally. Mostly because it doesn't have to do an extra > 0 comparison.
mysql> SELECT BENCHMARK(100000000,LOCATE('foo','foobar'));
+---------------------------------------------+
| BENCHMARK(100000000,LOCATE('foo','foobar')) |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
1 row in set (3.24 sec)
mysql> SELECT BENCHMARK(100000000,LOCATE('foo','foobar') > 0);
+-------------------------------------------------+
| BENCHMARK(100000000,LOCATE('foo','foobar') > 0) |
+-------------------------------------------------+
| 0 |
+-------------------------------------------------+
1 row in set (4.63 sec)
mysql> SELECT BENCHMARK(100000000,'foobar' LIKE '%foo%');
+--------------------------------------------+
| BENCHMARK(100000000,'foobar' LIKE '%foo%') |
+--------------------------------------------+
| 0 |
+--------------------------------------------+
1 row in set (4.28 sec)
mysql> SELECT ##version;
+----------------------+
| ##version |
+----------------------+
| 5.1.36-community-log |
+----------------------+
1 row in set (0.01 sec)
+1 to #Mchl for answering the question most directly.
But we should keep in mind that neither of the solutions can use an index, so they're bound to do table-scans.
Trying to decide between a cloth or plastic adhesive bandage is kind of silly, when you're trying to patch the hull of the Titanic.
For this type of query, one needs a full-text search index. Depending on the size of the table, this will be hundreds or thousands of times faster.
I did some tests as Mchi did.And I think it's hard to say which one is faster. It looks like depending on the first occurrence of the substring.
mysql> select benchmark(100000000, 'afoobar' like '%foo%');
+----------------------------------------------+
| benchmark(100000000, 'afoobar' like '%foo%') |
+----------------------------------------------+
| 0 |
+----------------------------------------------+
1 row in set (9.80 sec)
mysql> select benchmark(100000000, locate('foo', 'afoobar'));
+------------------------------------------------+
| benchmark(100000000, locate('foo', 'afoobar')) |
+------------------------------------------------+
| 0 |
+------------------------------------------------+
1 row in set (8.08 sec)
mysql> select benchmark(100000000, 'abfoobar' like '%foo%');
+-----------------------------------------------+
| benchmark(100000000, 'abfoobar' like '%foo%') |
+-----------------------------------------------+
| 0 |
+-----------------------------------------------+
1 row in set (10.55 sec)
mysql> select benchmark(100000000, locate('foo', 'abfoobar'));
+-------------------------------------------------+
| benchmark(100000000, locate('foo', 'abfoobar')) |
+-------------------------------------------------+
| 0 |
+-------------------------------------------------+
1 row in set (10.63 sec)
mysql> select benchmark(100000000, 'abcfoobar' like '%foo%');
+------------------------------------------------+
| benchmark(100000000, 'abcfoobar' like '%foo%') |
+------------------------------------------------+
| 0 |
+------------------------------------------------+
1 row in set (11.54 sec)
mysql> select benchmark(100000000, locate('foo', 'abcfoobar'));
+--------------------------------------------------+
| benchmark(100000000, locate('foo', 'abcfoobar')) |
+--------------------------------------------------+
| 0 |
+--------------------------------------------------+
1 row in set (12.48 sec)
mysql> select ##version;
+------------+
| ##version |
+------------+
| 5.5.27-log |
+------------+
1 row in set (0.01 sec)

How can I find the difference between two dates in MySQL?

How can I find the difference between two dates in MySQL?
DATEDIFF returns the difference in days between two dates:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
mysql> select current_date()>'2011-01-01 12:01:01';
+--------------------------------------+
| current_date()>'2011-01-01 12:01:01' |
+--------------------------------------+
| 1 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> select '2011-01-01 12:01:01' > current_date();
+----------------------------------------+
| '2011-01-01 12:01:01' > current_date() |
+----------------------------------------+
| 0 |
+----------------------------------------+
1 row in set (0.00 sec)
mysql>
learn DATEDIFF
eg : SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');

Automatically trimming extra zeros in mysql

mysql> select 28/4;
+--------+
| 28/4 |
+--------+
| 7.0000 |
+--------+
1 row in set (0.00 sec)
Is there a way I can set something system-wide to be more clever about this, or at least to automatically round every float to 2 places?
That's controlled by the div_precision_increment variable:
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_div_precision_increment
MySQL> select ceiling(28/4);
+---------------+
| ceiling(28/4) |
+---------------+
| 7 |
+---------------+
1 row in set (0.00 sec)
MySQL> select format(28/4,0);
+----------------+
| format(28/4,0) |
+----------------+
| 7 |
+----------------+
1 row in set (0.00 sec)
I GOT IT !!!
set div_precision_increment = 0;
MySQL> select 28/4;
+------+
| 28/4 |
+------+
| 7 |
+------+
1 row in set (0.00 sec)
You could also set it globally:
set global div_precision_increment = 0;
or add this to /etc/my.cnf
div_precision_increment=0
I think your answer, if it exists, lies on this page
I have to be honest, I read through the rules about how precision is applied and don't really understand how you end up with 5 digits of precision by diving 28 by 4.
Good luck!