SQL求和列值

时间:2017-06-24 20:22:43

标签: mysql sql sum

我有这样一张桌子:

<script type="text/javascript">
    google.charts.load('current', {'packages':['bar']});
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
        var data = google.visualization.arrayToDataTable([
            ['Programming Languages', 'Expertise Level %'],
            ['Java', 100],
            ['C', 80],
            ['Python', 100],
            ['Web UX', 100],
            ['Matlab', 70]

        ]);

        var options = {
            bars: 'vertical' // Required for Material Bar Charts.
        };

      var chart = new google.charts.Bar(document.getElementById('barchart_material'));

我想将最后两列合并为:

ID    Departure              Duration(in min)         
==============================================
1     2017-12-31 11:30:45     120
2     2017-06-16 22:31:46     240

我该怎么做?

出发是TIMESTAMP

持续时间INT

4 个答案:

答案 0 :(得分:1)

您需要使用ADDTIME和SEC_TO_TIME功能来完成工作

SELECT ADDTIME(Departure,SEC_TO_TIME(Duration*60)) as Arrival

了解更多 http://www.w3resource.com/mysql/date-and-time-functions/mysql-addtime-function.php

http://www.w3resource.com/mysql/date-and-time-functions/mysql-sec_to_time-function.php

答案 1 :(得分:0)

只需使用interval添加:

select t.*, (departure + interval duration minute) as arrival
from t;

如果上述方法无效(并且应该),您可以随时使用date_add()

select t.*, date_add(departure, interval duration minute) as arrival
from t;

答案 2 :(得分:0)

以下MySQL查询应该有效:

SELECT DATE_ADD("2017-12-31 11:30:45", INTERVAL 120 MINUTE);   

答案 3 :(得分:0)

您可以将DATE_ADD - 功能与INTERVAL MINUTE

结合使用
SELECT flights.*, DATE_ADD(departure, INTERVAL duration MINUTE) as arrival
from (
  SELECT '2017-12-31 11:30:45' AS departure, 120 as duration
  UNION SELECT '2017-06-16 22:31:46', 240) flights

rextester/mysql的输出:

    departure            duration   arrival
1   2017-12-31 11:30:45  120        2017-12-31 13:30:45
2   2017-06-16 22:31:46  240        2017-06-17 02:31:46