从以下查询的答案中删除重复的行

时间:2019-04-11 07:55:02

标签: sql sqlite

**按导演的电影数量降序列出所有导演5000或更多电影的导演

在d.name之前使用Distinct没有帮助。

result = pd.read_sql_query("SELECT d.name,count(*) as num 

FROM PERSON d, M_DIRECTOR md 

WHERE d.Pid = md.Pid 

GROUP BY d.Pid,d.name 

HAVING COUNT(*) >= 10

order by count(*) desc

",conn)

2 个答案:

答案 0 :(得分:1)

您必须在表之间使用适当的显式联接,并依靠不同的电影:

select 
  p.name,
  count(distinct d.mid) num
from person p
inner join m_director d on d.pid = p.pid
inner join movie m on m.mid = d.mid 
group by p.pid, p.name
having num >= 10
order by num desc

答案 1 :(得分:0)

Person表中可能有重复的记录-名称相同但ID不同的人。尝试仅按名称而不按ID分组

    result = pd.read_sql_query("SELECT d.name,count(*) as num 

FROM PERSON d, M_DIRECTOR md 

WHERE d.Pid = md.Pid 

GROUP BY d.name 

HAVING COUNT(*) >= 10

order by count(*) desc

",conn)