我有一个查询我试图“转换”为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.
希望此信息有所帮助。请注意,表格中提供了更多信息。这里只是一个片段。
答案 0 :(得分:3)
正如SELECT
Syntax所述:
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
请注意,id
,firstName
和lastName
为hidden columns,其值不确定,除非每条记录都相同。