如何在MySQL中的manpulated表中获取两列的最大值

时间:2016-06-01 06:27:11

标签: mysql mariadb

我有一张桌子,我操纵它来创建一些图表。这是原始表

    select * from traffic_user_daily;
    +-------------------+------------+-------+---------+----------+
    | username          | cdate      | chour | bytesin | bytesout |
    +-------------------+------------+-------+---------+----------+
    | 00:09:df:89:81:f2 | 2016-05-27 | 16    |   16469 |    10321 |
    | 00:09:df:89:81:f2 | 2016-05-27 | 18    |  599161 |   121796 |
    | 00:09:df:89:81:f2 | 2016-05-27 | 19    |   21058 |    11141 |
    | 00:09:df:89:81:f2 | 2016-05-28 | 08    |   20608 |    72061 |
    | 00:09:df:89:81:f2 | 2016-05-28 | 09    |  359375 |   176809 |

我像这样运行sıme查询:

    SELECT CONCAT(cdate,' ',chour,':00:00') AS ctime ,sum(bytesin*0.000002222) as totalKbpsin, sum(bytesout*0.000002222) as totalKbpsout FROM traffic_user_daily group by cdate,chour HAVING ctime >= now() - INTERVAL 1 DAY

    +---------------------+----------------+----------------+
    | ctime               | totalKbpsin    | totalKbpsout   |
    +---------------------+----------------+----------------+
    | 2016-05-31 10:00:00 | 1230.390102018 |  772.301406580 |
    | 2016-05-31 11:00:00 | 2554.907603688 | 1185.870290000 |
    | 2016-05-31 12:00:00 | 2260.437353658 |  295.407289650 |
    | 2016-05-31 13:00:00 | 3031.315984752 |  316.283035200 |
    | 2016-05-31 14:00:00 | 3872.374530486 |  720.599486178 |

但不知道如何获得totalKbpsin和totalKbpsout列的MAX值。 你帮帮我吗? 的问候,

4 个答案:

答案 0 :(得分:1)

使用您的示例数据,请尝试此操作;)

SELECT main.ctime, main.totalKbpsin, main.totalKbpsout
FROM(
    SELECT CONCAT(cdate,' ',chour,':00:00') AS ctime, SUM(bytesin*0.000002222) AS totalKbpsin, SUM(bytesout*0.000002222) AS totalKbpsout
    FROM traffic_user_daily
    GROUP BY cdate,chour
    HAVING ctime >= NOW() - INTERVAL 1 DAY) main
INNER JOIN (
    SELECT MAX(totalKbpsin) AS totalKbpsin
    FROM (
        SELECT CONCAT(cdate,' ',chour,':00:00') AS ctime, SUM(bytesin*0.000002222) AS totalKbpsin, SUM(bytesout*0.000002222) AS totalKbpsout
        FROM traffic_user_daily
        GROUP BY cdate,chour
        HAVING ctime >= NOW() - INTERVAL 1 DAY) tmp
) maxsin ON main.totalKbpsin = maxsin.totalKbpsin
UNION ALL
SELECT main.ctime, main.totalKbpsin, main.totalKbpsout
FROM(
    SELECT CONCAT(cdate,' ',chour,':00:00') AS ctime, SUM(bytesin*0.000002222) AS totalKbpsin, SUM(bytesout*0.000002222) AS totalKbpsout
    FROM traffic_user_daily
    GROUP BY cdate,chour
    HAVING ctime >= NOW() - INTERVAL 1 DAY) main
INNER JOIN (
    SELECT MAX(totalKbpsout) AS totalKbpsout
    FROM (
        SELECT CONCAT(cdate,' ',chour,':00:00') AS ctime, SUM(bytesin*0.000002222) AS totalKbpsin, SUM(bytesout*0.000002222) AS totalKbpsout
        FROM traffic_user_daily
        GROUP BY cdate,chour
        HAVING ctime >= NOW() - INTERVAL 1 DAY) tmp
) maxsout ON maxsout.totalKbpsout = main.totalKbpsout

答案 1 :(得分:1)

您可以使用GREATEST()函数。

select GREATEST(MAX(totalKbpsin),MAX(totalKbpsout)) from table_a

答案 2 :(得分:0)

我解决了。

$query_IO ="SELECT MAX(totalKbpsin),MAX(totalKbpsout) FROM (SELECT CONCAT(cdate,' ',chour,':00:00') AS ctime ,sum(bytesin*0.000002222) as totalKbpsin, sum(bytesout*0.000002222) as totalKbpsout FROM traffic_user_daily group by cdate,chour HAVING ctime >= now() - INTERVAL 1 DAY) a ";

答案 3 :(得分:0)

$ query_IO =“

SELECT  MAX(totalKbpsin),MAX(totalKbpsout)
    FROM  
      ( SELECT  CONCAT(cdate,' ',chour,':00:00') AS ctime ,sum(bytesin*0.000002222) as totalKbpsin,
                sum(bytesout*0.000002222) as totalKbpsout
            FROM  traffic_user_daily
            group by  cdate,chour
            HAVING  ctime >= now() - INTERVAL 1 DAY
      ) a 

“;

解决了我的问题。 为您提供帮助......