一個簡單的資料庫插入時間插入策略

來源:互聯網
上載者:User

最近寫一個項目,資料表插入資料的時候有一個特殊要求:

表結構屬性如下:id startime endtime value;

插入資料的要求就是插入的時間段starttime-endtime不能在已有記錄的starttime-endtime之間。

舉個例子:加入已有的記錄如下

1 3:00 5:00 10;2 6:00 8:00 20;3 10:00 14:00 15;4 16:00 10:00 20

那麼插入的資料不能是4:00-5:00,因為4:00已經在3:00-5:00時間段中,也不能是2:00-9:00,因為包含了3:00-5:00和6:00-8:00的時間段。

開始覺得有點麻煩,不過仔細想想還是很好操作。

首先排除記錄中所有endtime小於插入的st(新插入的starttime)的資料。用剩下的資料集來比較。

然後起始時間(st)大於starttime小於endtime的不得插入;

結束時間et(新插入的endtime)大於starttime小於endtime的不得插入;

起始時間(st)小於starttime,並且結束時間(et)大於endtime的不得插入;

最後在mybatis下的查詢不可插入的記錄的sql語句如下

<select id="getTimeToCompare" resultType="Time" parameterMap="paraMap">        <![CDATA[        select * from Time where id = #{id} and endtime > #{starttime} and         ((starttime <= #{starttime} and endtime >= #{starttime}) or        (starttime <= #{endtime} and endtime >= #{endtime}) or        (starttime >= #{starttime} and endtime <= #{endtime}))        ]]>    </select>

最後記錄一個mysql小異常的處理。最近在linux下面裝載了mysql後,如果用其他視覺化檢視遠端連線的時候,居然報Errorcode:13的錯誤,看意思是臨時目錄沒有許可權。於是我就把linux下的tmp目錄改成777許可權,居然可以訪問了。希望對遇到此問題的童鞋有點協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.