Mysql SQL选择具有其他实体的大多数实例的实体

Mysql SQL选择具有其他实体的大多数实例的实体,mysql,sql,database,select,many-to-many,Mysql,Sql,Database,Select,Many To Many,所以我有两个表“Subscription”和“Show”,它们通过多对多链接 我如何编写一个SQL脚本来告诉我哪个订阅包含的节目最多。显然,根据价格的不同,订阅包含不同数量的节目。您可以尝试以下查询: select s.id, count(ss.show_id) as showcount from subscription s, subscription_show ss where s.id = ss.subcription_id group by s.id, ss.show_id order

所以我有两个表“Subscription”和“Show”,它们通过多对多链接


我如何编写一个SQL脚本来告诉我哪个订阅包含的节目最多。显然,根据价格的不同,订阅包含不同数量的节目。

您可以尝试以下查询:

select s.id, count(ss.show_id) as showcount
from subscription s, subscription_show ss
where s.id = ss.subcription_id
group by s.id, ss.show_id
order by showcount desc
limit 1

我没试过这个,但我觉得可能有用

SELECT * from Subscription
WHERE id=(SELECT subscription_id
FROM Show_has_Subscription 
GROUP BY subscription_id
ORDER BY COUNT(subscription_id) DESC
LIMIT 1);

最简单的解决方案似乎不需要
连接
或子查询:

select ss.subcription_id, count(*) as showcount
from subscription_show ss
group by ss.subcription_id
order by showcount desc
limit 1;