0

我有一个查询,我正在尝试“转换”为 mysql。这是查询:

select top 5 * 
from
   (select id, firstName, lastName, sum(fileSize) as TotalBytes, sum(fileSize)/count(b.*)  as Average
    from roster_cs368 a
    join htmp_cs3868 b on b.id =  a.id
)
union
(
    select id, firstName, lastName, sum(fileSize) as TotalBytes, sum(fileSize)/count(b.*) as Average
    from roster_cs368 a
    join atmp_cs3868 b on b.id = a.id
)
order by TotalBytes desc

我希望有人可以向我展示“mysql:这样做的方式。我真的很感激。这是我正在做的 Java 程序的一部分,到目前为止还不太熟悉 sql 查询。

更新:

mysql> select * from roster_cs368
-> ;
+--------+-----------+-----------+
| id     | firstName | lastName  |
+--------+-----------+-----------+
| apn7cf | Allen     | Newton    |
| atggg3 | andrew    | goebel    |
| aysfgd | Alfred    | Santos    |
| cdq6c  | chris     | declama   |

其中“id”是主键

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                       |
| drwx---rwx |       26 | antognolij   | sasl     |     1683 | Nov   |   12 | 14:00 | 401                         |

这里也没有主键。

我要回答的问题是:

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.

希望这些信息有所帮助。请注意,表格中提供了更多信息。这里的只是一个片段。

4

1 回答 1

3

SELECT语法中所述:

LIMIT子句可用于限制SELECT语句返回的行数。

所以:

(
  SELECT id,
         firstName,
         lastName,
         SUM(fileSize) AS TotalBytes,
         SUM(fileSize)/COUNT(*) AS Average
  FROM   roster_cs368 AS a
    JOIN htmp_cs3868  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_cs3868  AS b USING (id)
)
ORDER BY TotalBytes DESC
LIMIT 5

请注意idfirstNamelastName隐藏列,其值是不确定的,除非每条记录都相同。

于 2012-12-02T16:45:22.027 回答