Count number of ID in relation and connect to name in PostgreSQL

klas mack

I have three tables: films, actors and the relationship actors_film.

I'm trying to decide which actors has been in the most number of movies. If the actor has been in a movie this is shown with actor_id(primary key).

I figured I want to count the number of times each actor_id shows in the table film_actor for each film_id, and then connect this with the right name(actor.first_name).

I've got the first part down, but can't seem to connect the Count-value to the right name.

SELECT actor_id, COUNT(*) AS number_of_films
FROM film_actor 
GROUP by actor_id 
ORDER by 2
DESC limit 1;

How would I go forward connecting the actors name from the actor-table?

I'm thinking I need some form of:

WHERE actor.actor_id = film_actor.actor_id. 
Paul L

You need a simple inner join between the table actors and the table actors_film:

  SELECT a.actor_name, a.actor_id, COUNT(*)
    FROM actors a INNER JOIN actors_films af
      ON a.actor_id = af.actor_id
GROUP BY a.actor_name, a.actor_id
ORDER BY 3;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count number of occurrences by Name

From Dev

Relation "table name" doesn't exist postgresql

From Dev

relation "table name" does not exist (postgresql)

From Dev

Laravel: Order query by count id field on relation

From Dev

PostgreSQL: count number of occurrences of a value in a column using the last entry per day, per name

From Dev

Count value based on name and number

From Dev

PostgreSQL convert month name to number

From Dev

Count number of rows for specific ID

From Dev

Count number of values per id

From Dev

Laravel hasMany relation count number of likes and comments on post

From Dev

PostgreSQL count number of times substring occurs in text

From Dev

How to count setof / number of keys of JSON in postgresql?

From Dev

retrieving name from number ID

From Dev

count number of files in directory with a certain name

From Dev

VBA Count Number of Worksheets with Specific Name (Easy)

From Dev

PostgreSQL "&" symbol and number after column name

From Dev

How to constrain saving id number as a value of field in ManyToOne Doctrine relation?

From Dev

Count generic DIV tags with no name or ID

From Dev

Count and define number of inputs with specific ID prefix

From Dev

COUNT a number of database rows affiliated with an id

From Dev

Count number of occcurance of a field for each ID

From Dev

PostgreSQL select all from one table and join count from table relation

From Dev

Adding variable to a number in jquery id name

From Dev

count number of records in db with id of another table id?

From Dev

Count Relation in Eloquent Query

From Dev

SQL - count relation instances

From Dev

Laravel relation total count

From Dev

PostgreSQL efficient count of number of rows WHERE parameter=TRUE

From Dev

PostgreSQL count max number of concurrent user sessions per hour

Related Related

  1. 1

    Count number of occurrences by Name

  2. 2

    Relation "table name" doesn't exist postgresql

  3. 3

    relation "table name" does not exist (postgresql)

  4. 4

    Laravel: Order query by count id field on relation

  5. 5

    PostgreSQL: count number of occurrences of a value in a column using the last entry per day, per name

  6. 6

    Count value based on name and number

  7. 7

    PostgreSQL convert month name to number

  8. 8

    Count number of rows for specific ID

  9. 9

    Count number of values per id

  10. 10

    Laravel hasMany relation count number of likes and comments on post

  11. 11

    PostgreSQL count number of times substring occurs in text

  12. 12

    How to count setof / number of keys of JSON in postgresql?

  13. 13

    retrieving name from number ID

  14. 14

    count number of files in directory with a certain name

  15. 15

    VBA Count Number of Worksheets with Specific Name (Easy)

  16. 16

    PostgreSQL "&" symbol and number after column name

  17. 17

    How to constrain saving id number as a value of field in ManyToOne Doctrine relation?

  18. 18

    Count generic DIV tags with no name or ID

  19. 19

    Count and define number of inputs with specific ID prefix

  20. 20

    COUNT a number of database rows affiliated with an id

  21. 21

    Count number of occcurance of a field for each ID

  22. 22

    PostgreSQL select all from one table and join count from table relation

  23. 23

    Adding variable to a number in jquery id name

  24. 24

    count number of records in db with id of another table id?

  25. 25

    Count Relation in Eloquent Query

  26. 26

    SQL - count relation instances

  27. 27

    Laravel relation total count

  28. 28

    PostgreSQL efficient count of number of rows WHERE parameter=TRUE

  29. 29

    PostgreSQL count max number of concurrent user sessions per hour

HotTag

Archive