我正在自学SQL程序GalaXQL(基于SQLite)中练习练习17.我有三张桌子:
Stars包含starid;Planets包含planetid和starid;Moons包含moonid和planetid.我想要返回starid与最大数量的行星和卫星相结合的相关联.
我有一个查询将返回starid,planetid和total行星+卫星.
如何更改此查询以使其仅返回与表starid对应的单个max(total)而不是表?这是我目前的查询:
select
stars.starid as sid,
planets.planetid as pid,
(count(moons.moonid)+count(planets.planetid)) as total
from stars, planets, moons
where planets.planetid=moons.planetid and stars.starid=planets.starid
group by stars.starid
Run Code Online (Sandbox Code Playgroud)