基于连接表的MySQL限制结果

时间:2016-10-08 08:51:15

标签: mysql sql many-to-many

我有2个表,但链接在很多关系中,所以有3个表:

Table Author :
idAuthor,
Name 

+----------+-------+
| idAuthor | Name  |
+----------+-------+
|        1 | Renee |
|        2 | John  |
|        3 | Bob   |
|        4 | Bryan |
+----------+-------+
Table Publication:
idPublication,
Title,
Type,
Date,
Journal,
Conference

+---------------+--------------+------+-------------+------------+-----------+
| idPublication | Title        | Date | Type        | Conference | Journal   |
+---------------+--------------+------+-------------+------------+-----------+
|             1 | Flower thing | 2008 | book        | NULL       | NULL      |
|             2 | Bees         | 2009 | article     | NULL       | Le Monde  |
|             3 | Wasps        | 2010 | inproceding | KDD        | NULL      |
|             4 | Whales       | 2010 | inproceding | DPC        | NULL      |
|             5 | Lyon         | 2011 | article     | NULL       | Le Figaro |
|             6 | Plants       | 2012 | book        | NULL       | NULL      |
+---------------+--------------+------+-------------+------------+-----------+
Table author_has_publication :
Author_idAuthor,
Publication_idPublication

+-----------------+---------------------------+
| Author_idAuthor | Publication_idPublication |
+-----------------+---------------------------+
|               1 |                         1 |
|               2 |                         2 |
|               3 |                         3 |
|               4 |                         4 |
|               1 |                         5 |
|               2 |                         5 |
|               3 |                         5 |
|               3 |                         6 |
+-----------------+---------------------------+

我想做的是让排名前X的作者拥有最多的出版物。

我使用此请求获得结果avec idAuthor拥有最多的出版物: SELECT Author_idAuthor, COUNT(*) as count FROM Author_has_publication GROUP BY Author_idAuthor ORDER BY count DESC; 我得到了作者ID列表,按出版物数量排序:

+-----------------+-------+
| Author_idAuthor | count |
+-----------------+-------+
|               3 |     3 |
|               2 |     2 |
|               1 |     2 |
|               4 |     1 |
+-----------------+-------+

但是当我尝试选择与上一个查询的结果集的前X对应的作者时,我有一个错误 我正在尝试这个SELECT TOP 2 FROM author WHERE (SELECT Author_idAuthor, COUNT(*) as count FROM Author_has_publication GROUP BY Author_idAuthor ORDER BY count DESC)=idAuthor; 我想这可能是因为我的内部查询返回2行,我在这里做了一个简单的SELECT或者我需要JOIN,但我不知道如何在这里使用它。

1 个答案:

答案 0 :(得分:1)

MySQL没有TOP关键字。但它有一个LIMIT关键字。无论如何,您的查询无效。

这里有几个选项。以下是相关子查询的示例:https://en.wikipedia.org/wiki/Correlated_subquery

SELECT 
    a.idAuthor, 
    a.Name , 
    (SELECT COUNT(*) from author_has_publication ahp WHERE   
        ahp.Author_idAuthor = a.idAuthor) AS publication_count
FROM 
    author a
ORDER BY 
    publication_count DESC
LIMIT 2

正如引用的文章所指出的,上面的效率很低,因为需要为结果的每一行重新执行子查询。如果您实际上不需要结果集中的计数,则下面的效率会更高,因为子查询是非相关的并且只执行一次。

SELECT 
    a.idAuthor, 
    a.Name 
FROM 
    author a
INNER JOIN 
    (select ahp.Author_idAuthor AS idAuthor, COUNT(*) as publication_count 
        FROM author_has_publication ahp GROUP BY ahp.Author_idAuthor LIMIT 2) 
        AS TEMP ON TEMP.idAuthor = a.idAuthor