sql between寫法關於查時間區間是否重疊

2021-09-07 05:42:02 字數 1124 閱讀 4805

--判斷是否重疊

select *from tablename

where starttime between

'2008-10-01

' and '

2008-10-07

'or endtime between

'2008-10-01

' and '

2008-10-07

'or

'2008-10-01

'between starttime and endtime

or '200-10-07

' between starttime and endtime;

--日期時間段查詢

--1.如果查詢日期引數為'

2017/02/21

',而資料庫表中的字段為'

2017/02/21 12:34:16.963

',則需要格式化一下日期才能查詢出來,如下

select * from table t where t.date between convert(datetime, '

2017/02/21

', 120) and convert(datetime, '

2017/02/21

', 120)+'

23:59:59

') ;

查詢的範圍為

'2017/02/21 00:00:00

'~'2017/02/21 23:59:59

',這樣就能解決問題。

-- 2

.或者使用dateadd方法,把日期加1天,如下

select * from table t where t.date >= convert(datetime, '

2017/02/21

') and t.date < convert(datetime, dateadd(day,1,'

2017/02/21

'));

查詢的範圍為

'2017/02/21

'<= t.date < '

2017/02/22

'

關於auto increment的寫法

以前不知道資料庫可以自己維護主鍵的,後來在網上查了,才知道。下面是對mysql資料庫的!首先建立表結構如下 create table t user website id integer 5 not null auto increment name varchar 50 not null,primar...

關於if else語句的寫法

假如現在我們的情況判斷確定只有兩種 當然未來可能會有新的情況 比如 正確和錯誤兩種狀態。我們定義正確的status值為1,錯誤的status值為2。不推薦的 寫法 public static void main string args else 應該這麼寫 public static void ma...

關於遞迴寫法的精妙記錄

之前對遞迴的了解就限制於 n!之類的用法,在學習python的過程中,接觸到了關於漢諾塔的移動的遞迴介紹,覺得遞迴實在是神奇。這種思想的迸發我希望能記錄下來,並激勵。def move n,a,b,c if n 1 print a,c return move n 1,a,c,b print a,c m...