Q: できれば 1 回のクエリでアイテムの残りのスペースの合計を取得したい
Grops, items
Group can contain only MaxAllowed items
Groups table
(ID, MAXAllowerdItems)
Items
(ID, Group_ID, Name)
これは正しいクエリではありませんが、出発点です
select SUM(g.MaxAllowedItems - count(*)),
from FROM items i, Groups g
where g.ID=i.Group_ID
GROUP BY i.Group_ID
HAVING g.MaxAllowedItems > count( * )