sql上的多选查询

时间:2016-02-11 15:54:49

标签: mysql

我有下表 -

mysql> select * from appdata;
+------------+----------+-----------+
| timestamp  | appName  | Bytes     |
+------------+----------+-----------+
| 1454169299 | DNS      |    123456 |
| 1454169292 | DNS      |     12342 |
| 1454169292 | Facebook |   1234222 |
| 1454169297 | Youtube  | 123422233 |
+------------+----------+-----------+

I wanted to sum the byte and show the single application and consumed bytes

我执行了

mysql> select appName,Sum(Bytes) from appdata group by appName;
+----------+------------+
| appName  | Sum(Bytes) |
+----------+------------+
| DNS      |     135798 |
| Facebook |    1234222 |
| Youtube  |  123422233 |
+----------+------------+

现在我想运行一个查询,它为我提供了消耗更多字节的应用程序

youtube  123422233
Facebook 1234222
DNS 135798

我正在运行

select appName,Sum(Bytes) from appdata group by appName
    (select appName,Bytes FROM appdata ORDER BY Bytes desc limit 3);

但它没有用,请让我知道如何实现这一目标。

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT appname, SUM(bytes) FROM appdata GROUP BY appname ORDER BY SUM(bytes) DESC LIMIT 3;

您需要按字节数下降的顺序排序,这样您将获得最高的数字。