How can I make an average of dates in MySQL?

前端 未结 7 2132
感情败类
感情败类 2020-12-01 20:42

How can I make an average between dates in MySQL? I am more interested in the time values, hours and minutes.

On a table with:

| date_one   | datetime          


        
7条回答
  •  醉酒成梦
    2020-12-01 21:11

    This seems a bit hackish, but will work for dates beteen ~ 1970 and 2030 (on 32 bit arch). You are essentially converting the datetime values to integer, averaging them, and converting the average back to a datetime value.

    SELECT
        from_unixtime(
            avg(
                unix_timestamp(date_one)-unix_timestamp(date_two)
            )
        )
    FROM
        some_table
    WHERE
        some-restriction-applies
    

    There is likely a better solution out there, but this will get you by in a pinch.

提交回复
热议问题