需要来自此查询的“前五名”ID

时间:2012-12-02 22:07:03

标签: java mysql

我之前有人帮助我并且已经接近了,我需要从这个查询获得前五名“id”。正在发生的事情是我正在获得一个身份而不是前五名。有谁知道如何纠正这个?这是查询:

(
SELECT id,
     firstName,
     lastName,
     SUM(fileSize) AS TotalBytes,
     SUM(fileSize)/COUNT(*) AS Average
FROM   roster_cs368 AS a
JOIN htmp_cs368  AS b USING (id)
) UNION (
SELECT id,
     firstName,
     lastName,
     SUM(fileSize) AS TotalBytes,
     SUM(fileSize)/COUNT(*) AS Average
FROM   roster_cs368 AS a
JOIN atmp_cs368  AS b USING (id)
)
ORDER BY TotalBytes DESC
LIMIT 5

谢谢!这也是一个更大的程序的一部分,因此是java标记。

这是架构:

mysql> select * from roster_cs368
-> ;
+--------+-----------+-----------+
| id     | firstName | lastName  |
+--------+-----------+-----------+
| apn7cf | Allen     | Newton    |
| atggg3 | andrew    | goebel    |

主键在哪里,

mysql> select * from htmp_cs368;
+------------+----------+------------+----------+----------+-------+------+-------+----------------------+
| filePerms  | numLinks | id         | idGroup  | fileSize | month | day  | time  | fileName             |
+------------+----------+------------+----------+----------+-------+------+-------+----------------------+
| drwx------ |        2 | schulte    | faculty  |      289 | Nov   |    7 | 2011  | Java                 |
| -rw-r--r-- |        1 | schulte    | faculty  |      136 | Apr   |   29 | 2012  | LD                   |
| drwxr-xr-x |        3 | schulte    | faculty  |      177 | Mar   |   20 | 2012  | Upgrade              |

这里没有主键,

mysql> select * from atmp_cs368;
+------------+----------+--------------+----------+----------+-------+------+-------+-----------------------------+
| filePerms  | numLinks | id           | idGroup  | fileSize | month | day  | time  | fileName                    |
+------------+----------+--------------+----------+----------+-------+------+-------+-----------------------------+
| drwxr-xr-x |        2 | remierm      | 203      |      245 | Sep   |   17 | 14:40 | 148360_sun_studio_12        |
| drwx---rwx |       31 | antognolij   | sasl     |     2315 | Oct   |   24 | 12:28 | 275                         |
| -rwx------ |        1 | kyzvdb       | student  |       36 | Sep   |   19 | 13:05 | 275hh                       |

并且没有主键。

我的疑问是什么:

produce a list of the five members of roster_cs368,
    and their ids who use the most space (number of bytes)
    in htmp_cs368 and atmp_cs368 in descending order--
    greediest first.  display total number of bytes and
    average size of file

1 个答案:

答案 0 :(得分:0)

你可以这样做:

SELECT * FROM(
(
SELECT id,
     firstName,
     lastName,
     SUM(fileSize) AS TotalBytes,
     SUM(fileSize)/COUNT(*) AS Average
FROM   roster_cs368 AS a
JOIN htmp_cs368  AS b USING (id)
) UNION (
SELECT id,
     firstName,
     lastName,
     SUM(fileSize) AS TotalBytes,
     SUM(fileSize)/COUNT(*) AS Average
FROM   roster_cs368 AS a
JOIN atmp_cs368  AS b USING (id)
)
) as somealias ORDER BY TotalBytes DESC
LIMIT 5