All you need to do is get the package_id from dotlrn_communities_all. The community_id is the group_id.
It looks like you are trying to find the dotlrn community group associated with an application mounted under a dotlrn package instance.
You can use someting like this
set community_id [dotlrn_community::get_community_id -package_id $package_id]
This uses the site node table which is cached in memory so a database hit is not necessary.
Application groups are usually used to map one group to one package. Not to all the packages underneath it. Basically the dotlrn_communities_all table duplicates the application group mapping used for subsites. If you mount packages under a subsite, they do not have an application group mapping either.