A simple database insertion time insertion strategy

Source: Internet
Author: User

When you write a project recently, a data table inserts data with a special request:

Table structure properties are as follows: ID startime endtime value;

The requirement for inserting data is that the inserted time period starttime-endtime not between the starttime-endtime of an existing record.

For example: Add an existing record as follows

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

The inserted data cannot be 4:00-5:00, since 4:00 is already in the 3:00-5:00 time period and cannot be 2:00-9:00, because it contains the 3:00-5:00 and 6:00-8:00 time periods.

It's starting to feel a little bit of trouble, but it's good to think about it.

First excludes all endtime in the record that are less than the inserted St (newly inserted starttime) data. Compare with the rest of the dataset.

Then the starting time (ST) is greater than the StartTime less than the Endtime;

The end time ET (newly inserted endtime) is greater than starttime less than endtime;

The starting Time (ST) is less than starttime, and the end Time (ET) is greater than endtime;

The SQL statement for the last record that is not inserted in the MyBatis query is as follows

<select id= "Gettimetocompare" resulttype= "Time" parametermap= "Paramap" >
        <![ cdata[
        SELECT * FROM time where id = #{id} and Endtime > #{starttime} and 
        (StartTime <= #{starttime} and EN Dtime >= #{starttime}) or
        (StartTime <= #{endtime} and Endtime >= #{endtime}) or StartTime >=
        Time} and Endtime <= #{endtime})
        ]]>
    </select>

Finally, the processing of a small MySQL exception is recorded. Recently after the Linux loaded with MySQL, if the remote connection with other visual tools, actually reported errorcode:13 error, see the meaning of the temporary directory does not have permissions. So I changed the TMP directory under Linux to 777 permissions, and I could access it. I hope I have a little help with the children's shoes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.