0

我早些时候有人帮助我并且已经接近,我需要从这个查询中获得前五个“id”。正在发生的事情是我正在获得一个 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
4

1 回答 1

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
于 2012-12-02T22:10:18.703 回答