错误。说film_id必须出现在GROUP BY子句中

时间:2017-08-15 19:12:00

标签: postgresql

SELECT  film_id, film_actor.actor_id,first_name,last_name,COUNT(*) 
FROM film_actor 
INNER JOIN actor ON film_actor.actor_id=actor.actor_id 
GROUP BY film_actor.actor_id  ;

1 个答案:

答案 0 :(得分:0)

SELECT 
    film_id,?     ---(Which table ? you can neglect) 
    film_actor.actor_id,
    first_name,
    last_name,
    COUNT(*) AS Total 
FROM film_actor 
INNER JOIN actor 
ON film_actor.actor_id =actor.actor_id 
GROUP BY film_actor.actor_id ;

此SQL语句的最佳实践将类似于

SELECT
  c.film_id,
  a.actor_id,
  b.first_name,
  b.last_name
  a.Count
FROM
    (
      SELECT 
            film_actor.actor_id,
            COUNT(*) AS Count 
        FROM film_actor 
        INNER JOIN actor 
        ON film_actor.actor_id =actor.actor_id 
        GROUP BY film_actor.actor_id ;
    ) AS a
INNER JOIN actor b ON b.actor_id = a.actor_id
INNER JOIN film_actor c ON c.actor_id = a.actor_id