MYSQL entries from one table that appear most often in another table

Broom

I have 2 tables, authors and books

authors contains the unique id authorId

books also contains this as a foreign key

I need to know the authors with the most number of books. If 2 or more authors are tied for the greatest number of books, I need to show both authors

I have been able to achieve this by first getting the maximum count

SELECT @maxCount := (MAX(counter)) FROM (SELECT count(*) AS counter FROM books GROUP BY authorId) AS counts;

and then using it to get the Ids with that count as part of my author selection

SELECT * 
FROM authors 
WHERE authorId IN (
    SELECT authorId 
    FROM books 
    GROUP BY authorId 
    HAVING COUNT(*) = @maxCount
);

I've been told that I am not allowed to use variables and that what I've done is horribly inefficient if the tables grow very large.

Am I missing something obvious here? Is there a way to do this in a single statement without a variable (or temp table), and without having to select/group the entire books table twice?

symcbean
SELECT author, COUNT(*)
FROM authors
JOIN books
ON authors.authorId=books.AuthorId
GROUP BY author
ORDER BY COUNT(*) DESC

Will give you a list ordered by the number of books for each author. I don't have an instance nearby to test, and tend to steer clear of embedded variables but expect something like....

SELECT *
FROM (
  SELECT author
  , @maxcount:=IF(COUNT(*)>@maxcount,COUNT(*), @maxcount) 
  , COUNT(*) AS cnt
  FROM authors
  JOIN books
  ON authors.authorId=books.AuthorId
  GROUP BY author
  ORDER BY COUNT(*) DESC
) ilv
WHERE cnt=@maxcount;

Performance still sucks with large datasets (even with the right indexes). If you have to run this query frequently with >100,000 records, then you might consider denormalizing your data.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL query most recent records from one table but in the order from another table

From Dev

Select most recent entries and join with another table

From Dev

Select all entries from one table which has two specific entries in another table

From Dev

Selecting entries from a table based on another table

From Dev

Data from one table to another in MySQL

From Dev

mysql - move rows from one table to another

From Dev

Move data from one MySQL table to another

From Dev

Validate fields from one table to another in MySQL

From Dev

Moving data from one mysql table to another

From Dev

Move data from one MySQL table to another

From Dev

MySQL taking from one table into another

From Dev

MySQL add data from one table to another

From Dev

Insert Data From One Table To Another - MySQL

From Dev

msaccess join most recent matching record from one table to another

From Dev

msaccess join most recent matching record from one table to another

From Dev

MySQL select * from one table based on stock from another table

From Dev

How to Replace and Update Data From One Table to Another Table in MySQL

From Dev

MySQL - Exclude all rows from one table if match on another table

From Dev

MYSQL, copy data from one table to another table with increment in varchar

From Dev

Mysql Trigger, Copy All Rows From One Table Into Another Table

From Dev

Update mysql one table from another table based on dates

From Dev

MYSQL Copy field from one table to another table

From Dev

How to Replace and Update Data From One Table to Another Table in MySQL

From Dev

PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

From Dev

PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

From Dev

List names from one table whose ID matches entries in another table

From Dev

How do I join the most recent row in one table to another table MYSQL

From Dev

Join one table with the most recent rows of another

From Dev

Store dynamic number of entries in one table to a single row in another table

Related Related

  1. 1

    MySQL query most recent records from one table but in the order from another table

  2. 2

    Select most recent entries and join with another table

  3. 3

    Select all entries from one table which has two specific entries in another table

  4. 4

    Selecting entries from a table based on another table

  5. 5

    Data from one table to another in MySQL

  6. 6

    mysql - move rows from one table to another

  7. 7

    Move data from one MySQL table to another

  8. 8

    Validate fields from one table to another in MySQL

  9. 9

    Moving data from one mysql table to another

  10. 10

    Move data from one MySQL table to another

  11. 11

    MySQL taking from one table into another

  12. 12

    MySQL add data from one table to another

  13. 13

    Insert Data From One Table To Another - MySQL

  14. 14

    msaccess join most recent matching record from one table to another

  15. 15

    msaccess join most recent matching record from one table to another

  16. 16

    MySQL select * from one table based on stock from another table

  17. 17

    How to Replace and Update Data From One Table to Another Table in MySQL

  18. 18

    MySQL - Exclude all rows from one table if match on another table

  19. 19

    MYSQL, copy data from one table to another table with increment in varchar

  20. 20

    Mysql Trigger, Copy All Rows From One Table Into Another Table

  21. 21

    Update mysql one table from another table based on dates

  22. 22

    MYSQL Copy field from one table to another table

  23. 23

    How to Replace and Update Data From One Table to Another Table in MySQL

  24. 24

    PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

  25. 25

    PostgreSQL count entries from one table for a given id and mutliple that by rates in another table and sum it all up

  26. 26

    List names from one table whose ID matches entries in another table

  27. 27

    How do I join the most recent row in one table to another table MYSQL

  28. 28

    Join one table with the most recent rows of another

  29. 29

    Store dynamic number of entries in one table to a single row in another table

HotTag

Archive