rabbbit
V2EX  ›  问与答

关于 sqlzoo SQL 的问题

  •  
  •   rabbbit · Feb 6, 2022 · 840 views
    This topic created in 1573 days ago, the information mentioned may be changed or developed.

    sqlzoo 第 12 题, 查找某演员出演电影次数最多的年份 https://sqlzoo.net/wiki/More_JOIN_operations/zh

    我这么写对吗?为啥答案搞的这么绕,性能更好吗,还是为了用 join 而用?

    这是我写的

    select yr, count(*) as c from movie
    where id in (
      select movieid from casting 
      where actorid=(select id from actor where name='John Travolta')
    )
    group by yr order by c desc limit 1
    

    这是答案

    SELECT yr,COUNT(title) FROM movie 
      JOIN casting ON movie.id=movieid
      JOIN actor ON actorid=actor.id
    where name='John Travolta'
    GROUP BY yr
    HAVING COUNT(title)=(SELECT MAX(c) FROM
    (SELECT yr,COUNT(title) AS c FROM movie 
     JOIN casting ON movie.id=movieid
     JOIN actor   ON actorid=actor.id
     where name='John Travolta'
     GROUP BY yr) AS t
    )
    
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2643 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 12:23 · PVG 20:23 · LAX 05:23 · JFK 08:23
    ♥ Do have faith in what you're doing.