最近寫一個項目,資料表插入資料的時候有一個特殊要求:
表結構屬性如下: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許可權,居然可以訪問了。希望對遇到此問題的童鞋有點協助。