简体   繁体   中英

MySQL: Output datetime field in query by combining two fields

I have two fields.

  1. teststartdatetime - datatype: datetime, start of test.
  2. testendtime - datatype: mediumtext, text of endtime in "00:00:00" format

I need to query my db to output a new field testenddatetime (as datetime datetype) which contains both the time part of the testendtime field and the date from the teststartdatetime field.

Also, I need it to check that the value of the testendtime field is later than the value of the time portion of the teststartdatetime field. And if not then increase the testenddatetime by one day.

Note: Tests are no longer than 24hrs

Feel free to play around with it at SQL Fiddle

Does concat() do what you want?

select concat(date(teststartdatetime), ' ', testendtime)

Storing times as a string is a really bad idea because MySQL has a time datatype.

As for the check:

select (case when time(teststartdatetime) > time(testendtime)
             then concat(date(teststartdatetime) + interval 1 day, ' ', testendtime)
             else concat(date(teststartdatetime), ' ', testendtime)
        end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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