Computer >> Máy Tính >  >> Lập trình >> MySQL

Toán tử cộng, trừ, nhân và chia sẽ hoạt động như thế nào với các giá trị ngày tháng được lưu trữ trong bảng MySQL?

Khi chúng tôi cố gắng thực hiện loại hoạt động như vậy với các giá trị ngày được lưu trữ trong bảng thì MySQL sẽ giả định các giá trị ngày là số và thực hiện số học.

Giả sử chúng ta có một bảng tên là ‘example’ có giá trị ngày trong cột ‘orderdate’ thì việc làm theo phép toán số học sẽ làm rõ điều trên -

mysql> select * from example;
+------------+
| orderdate  |
+------------+
| 2017-05-25 |
+------------+
1 row in set (0.00 sec)

mysql> select orderdate+10 from example;
+--------------+
| orderdate+10 |
+--------------+
|     20170535 |
+--------------+
1 row in set (0.00 sec)

mysql> select orderdate*10 from example;
+--------------+
| orderdate*10 |
+--------------+
|    201705250 |
+--------------+
1 row in set (0.00 sec)

mysql> select orderdate-10 from example;
+--------------+
| orderdate-10 |
+--------------+
|     20170515 |
+--------------+
1 row in set (0.00 sec)

mysql> select orderdate/10 from example;
+--------------+
| orderdate/10 |
+--------------+
|    2017052.5 |
+--------------+
1 row in set (0.00 sec)