mysql 日期時間類型 自動轉型 及 運算

日期時間類型自動轉型

--?now()、字符串、數字轉datetime類型

create table t(dt datetime);
insert into t values(now());
insert into t values('2007-9-3 12:10:10');
insert into t values('2007/9/3 12+10+10');
insert into t values('2007#9#3 12+10+10');
insert into t values('2007+9+3 12+10+10');
insert into t values('20070903121010');
insert into t values(20080903121010);

--?now()、字符串、數字轉date類型
create table test(dt date);
insert into test values(now());
insert into test values('2007-9-3 12:10:10');
insert into test values('2007/9/3 12+10+10');
insert into test values('2007#9#3 12+10+10');
insert into test values('2007+9+3 12+10+10');
insert into test values('20070903121010');
insert into test values(20080903121010);

--?now()、字符串、數字轉time類型
create table t3(dt time);
insert into t3 values(now());
insert into t3 values('2007-9-3 12:10:10');
insert into t3 values('2007/9/3 12+10+10');
insert into t3 values('2007#9#3 12+10+10');
insert into t3 values('2007+9+3 12+10+10');
insert into t3 values('20070903121010');
insert into t3 values(20080903121010);

--?now()、字符串、數字轉timestamp類型
create table t4(dt timestamp);
insert into t4 values(now());
insert into t4 values('2007-9-3 12:10:10');
insert into t4 values('2007/9/3 12+10+10');
insert into t4 values('2007#9#3 12+10+10');
insert into t4 values('2007+9+3 12+10+10');
insert into t4 values('20070903121010');
insert into t4 values(20080903121010);

--?在任何時間,now()、任意分隔符的年月日時分秒字符串、年月日時分秒數字串都可以拿來當日期類型、時間戳使用。但月日時分秒要保持2位數。

--?日期類型可以轉時間戳,時間戳不能轉時期類型

drop table test;
create table test(dt date);
insert into test values(unix_timestamp()); --? 報錯

drop table test;
create table test(dt timestamp);
insert into test values(curdate());


--?加減運算

--?now(),sysdate(),current_timestamp(),curdate(),curtime() 函數和日期時間類字段都可以加特定格式的數字
select now(),now()+1,now()-1,now()+101,now()+050000;
--?加1秒,減1秒,加1分1秒,加5小時,不會報錯,但最后一個結果有問題:20180714265220,26點是錯的
--?2018-07-14 21:52:20, 20180714215221, 20180714215219, 20180714215321, 20180714265220
select sysdate()+1, current_timestamp()+1, curtime()+1, curdate()+1; --? 最后這個是加一天
-- 20180714215353, 20180714215353, 215353, 20180715

drop table test;
create table test(id datetime);
insert into test values(now()+010000);
insert into test values(now()+050000);? -- 報錯,因為現在是21:48,加上5小時就成26點了就不對了,他只會自顧自地往上加

drop table test;
create table test(id timestamp);
insert into test values(now());
select id, id+1, id+101, id+010000 from test;

drop table test;
create table test(id date);
insert into test values(now());
select id, id+1, id+101, id+010000 from test;


--?比較運算

-- 日期類型

-- datetime類型

drop table test;
create table test(id datetime);
insert into test values(now());
select * from test where id > '1999-1-1'; //結果:2018-07-13 03:38:35
select * from test where id > '1999=1=1'; //結果:2018-07-13 03:38:35
select * from test where id > '1999'; //結果:2018-07-13 03:38:35
select * from test where id > 1999; //結果:2018-07-13 03:38:35
select * from test where id < now(); //結果:2018-07-13 03:38:35

-- year 類型

drop table test;
create table test(id year);
insert into test values(now());
select * from test where id > '1999-1-1'; //結果:2018
select * from test where id > '1999=1=1'; //結果:2018
select * from test where id > '1999'; //結果:2018
select * from test where id > 1999; //結果:2018
select * from test where id > '1999=1=1'; //結果:2018
select * from test where id < now();

-- timestamp 類型

drop table test;
create table test(id timestamp);
insert into test values(now());
select * from test where id > '1999-1-1'; //結果:2018-07-13 03:44:44
select * from test where id < now(); //結果:2018-07-13 03:44:44

轉載于:https://www.cnblogs.com/Mike_Chang/p/9307622.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/283934.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/283934.shtml
英文地址,請注明出處:http://en.pswp.cn/news/283934.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

.NET Community Toolkit 8.0.0 版本發布

.NET 社區工具包&#xff08;.NET Community Toolkit &#xff09;現已發布 8.0.0 版&#xff01;.NET 社區工具包是一組適用于所有 .NET 開發人員&#xff0c;且與任何特定 UI 平臺無關的幫助程序和 API。該工具包由 Microsoft 維護和發布&#xff0c;是 .NET 基金會的一部分&…

SpringData JPA、Hibernate、Mybatis三者的區別

目錄 1.ORM 考慮 SpringData JPA Hibernate MyBatis 2.業務查詢的區別 Spring Data JPA Hibernate Mybatis 3.可拓展性 Spring Data JPA Hibernate Mybatis 4.對緩存 Spring Data JPA Hibernate Mybatis 5.難度性 Spring Data JPA Hibernate Mybatis 總述…

1、內存

程序為什么需要內存 程序運行的目的&#xff1a;程序運行是為了得到一定的結果&#xff0c;程序運行其實是在做一系列的數據計算&#xff0c;所以&#xff1a;程序代碼數據&#xff1b;程序運行的目的不外乎2個&#xff1a;過程、結果&#xff1b; 用函數來類比&#xff1a;…

Map 遍歷取值及jstl的取值

Map 遍歷取值及jstl的取值 學習了&#xff1a;http://blog.csdn.net/yanjiaye520/article/details/17354239 1、Java map的便利取值 Java代碼 收藏代碼 Map<String,String> map new HashMap<String,String>(); map.put("key1", "value1");…

基于CAP組件實現補償事務與冪等性保障

【.NET Core】| 總結/Edison Zhou1補償事務和冪等性在微服務架構下&#xff0c;我們會采用異步通信來對各個微服務進行解耦&#xff0c;從而我們會用到消息中間件來傳遞各個消息。 補償事務某些情況下&#xff0c;消費者需要返回值以告訴發布者執行結果&#xff0c;以便于發布者…

Docker與k8s

前言 隨著k8s 作為容器編排解決方案變得越來越流行&#xff0c;有些人開始拿 Docker 和 k8s進行對比&#xff0c;不禁問道&#xff1a;Docker 不香嗎&#xff1f; k8s 是kubernets的縮寫&#xff0c;’8‘代表中間的八個字符。 其實 Docker 和 k8s 并非直接的競爭對手&#xff…

Linux下啟動tomcat報java.lang.OutOfMemoryError: PermGen space

2019獨角獸企業重金招聘Python工程師標準>>> 一、錯誤信息 java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav…

Redis安裝[Windows]

一. redis下載地址: https://github.com/ServiceStack/redis-windows/tree/master/downloads 根據需要的下載對應版本*.zip即可.(我這里是win7x64) 二.使用 1. 下載之后解壓到你相應的目錄下: 1 文件介紹&#xff1a; 2 redis-benchmark.exe #基準測試 3 redis-check-aof.e…

簡練軟考知識點整理-項目啟動過程組

啟動過程組包含定義一個新項目或現有項目的一個新階段&#xff0c;授權開始該項目或階段的一組過程。在啟動過程中&#xff0c;定義初步范圍和落實初步財務資源&#xff0c;識別那些將相互作用并影響項目總體結果的內外部干系人&#xff0c;選定項目經理&#xff08;如果尚未安…

在 ASP.NET Core 中上傳文件

簡介文件上傳是指將媒體文件&#xff08;本地文件或網絡文件&#xff09;從客戶端上傳至服務器存儲。ASP.NET Core 支持使用緩沖的模型綁定&#xff08;針對較小文件&#xff09;和無緩沖的流式傳輸&#xff08;針對較大文件&#xff09;上傳一個或多個文件。緩沖和流式傳輸是上…

Paxos算法詳解

Paxos、Raft分布式一致性算法應用場景一文講述了分布式一致性問題與分布式一致性算法的典型應用場景。作為分布式一致性代名詞的Paxos算法號稱是最難理解的算法。本文試圖用通俗易懂的語言講述Paxos算法。 一、Paxos算法背景 Paxos算法是Lamport宗師提出的一種基于消息傳遞的分…

LeetCode 322. Coin Change

原題 You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, …

Teiid:數據虛擬化Data Virtualization平臺

2019獨角獸企業重金招聘Python工程師標準>>> Teiid介紹 http://teiid.jboss.org/ 數據虛擬化的定義 https://en.wikipedia.org/wiki/Data_virtualization http://www.denodo.com/en/data-virtualization/overview 數據虛擬化的文章 Sick of ETL? Database virtuali…

如何仿造一個websocket請求?

之前兩次singnalr、 websocket實時推送相關&#xff1a;? .NET WebSockets 核心原理初體驗[1]? SignalR 從開發到生產部署避坑指南[2]tag&#xff1a;瀏覽器--->nginx--> server其中提到nginx默認不會為客戶端轉發Upgrade、Connection標頭&#xff0c; 因為為了讓被代理…

【轉】為什么自動車完全不可以犯錯誤

為什么自動車完全不可以犯錯誤 有人跟我講&#xff0c;我對Google的自動車要求太苛刻了。人無完人&#xff0c;所以Google的產品也不需要是完美的&#xff0c;只要“夠好用”就有市場。世界上有那么多糟糕的司機&#xff0c;酒后駕車的&#xff0c;開車時發短信的&#xff0c;打…

從“互聯網+教育”到“教育+互聯網”——互聯網文化基因視域下的審思

作者信息 朱敬/廣西師范大學教育學部教授&#xff0c;教育學博士&#xff0c;博士生導師&#xff1b; 蔡建東/河南大學教育學部教授&#xff0c;教育學博士。 本文摘要 近年來國務院與教育部文件逐漸使用“教育互聯網”一詞&#xff0c;從“互聯網教育”到“教育互聯網”&a…

Node.js Stream - 基礎篇

背景 在構建較復雜的系統時&#xff0c;通常將其拆解為功能獨立的若干部分。這些部分的接口遵循一定的規范&#xff0c;通過某種方式相連&#xff0c;以共同完成較復雜的任務。譬如&#xff0c;shell通過管道|連接各部分&#xff0c;其輸入輸出的規范是文本流。 在Node.js中&am…

Axure RP使用攻略--動態面板的用途(8)

寫了幾個Axure教程之后發現&#xff0c;可能教程的起點有些高了&#xff0c;過分的去講效果的實現&#xff0c;而忽略了axure功能以及基礎元件的使用&#xff0c;那么從這個教程開始&#xff0c;把這些逐漸的展開講解。 關于動態面板 動態面板是axure原型制作中使用非常頻繁的一…

ABP 6.0.0-rc.1的新特性

2022-07-26官方發布ABP 6.0.0-rc.1版本&#xff0c;本文挑選了幾個新特性進行了介紹&#xff0c;主要包括LeptonX Lite默認主題、OpenIddict模塊&#xff0c;以及如何將Identity Server遷移到OpenIddict。據ABP官方公眾號介紹&#xff0c;ABP 6.0.0穩定版的計劃發布日期為2022-…

Java并發包--線程池框架

轉載請注明出處&#xff1a;http://www.cnblogs.com/skywang12345/p/3509903.html 線程池架構圖 線程池的架構圖如下&#xff1a; 1. Executor 它是"執行者"接口&#xff0c;它是來執行任務的。準確的說&#xff0c;Executor提供了execute()接口來執行已提交的 Runna…