简体   繁体   English

在两次之间插入持续时间..在mysql中

[英]Inserting the duration between two times..in mysql

I am inserting two time values in the mysql database columns TIME1 and TIME2 respectively and at the same time i also want the duration between the two times to be calculated and be inserted into a DURATION named column..I tried some sub queries but being weak with database coding i am not able to do this..Also Queries are being written in the java classes not in sql command line..Values are supposed to be entered in the same database.. 我分别在mysql数据库列TIME1和TIME2中插入两个时间值,同时我还希望计算两次之间的持续时间,并将其插入到DURATION命名列中。使用数据库编码,我无法做到这一点。此外,查询也在Java类中而不是在sql命令行中编写。应该在同一个数据库中输入值。

Insert into meeting_hall_booking_master(booking_duration)
select TIMEDIFF(booking_to_time,booking_from_time) from meeting_hall_booking_master;

This query inserts a new row with the booking duration calulated with the help of the values of TIME1 and TIME2 of its previous row. 该查询将插入一个新行,该行的预订持续时间将根据前一行的TIME1和TIME2的值进行计算。

Use update statement it will solve your problem 使用更新语句将解决您的问题

update meeting_hall_booking_master 
set booking_duration = TIMEDIFF(booking_to_time,booking_from_time);

It seems you actually need an update statement now. 看来您现在实际上需要一个update语句。

update meeting_hall_booking_master
 set booking_duration = TIMEDIFF( booking_to_time, booking_from_time )

Instead of updating on a later stage, you can set the duration value in the insert statement. 您可以在insert语句中设置持续时间值,而不必在以后进行更新。

insert into 
 meeting_hall_booking_master( booking_to_time, booking_from_time, booking_duration )
 values ( @booking_to_time,
          @booking_from_time,
          timediff( @booking_to_time, @booking_from_time )
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM