So i need to find the tribe (table called pleme) with highest population.. to get it, i need to combine the folowing 3 tables: naselje and igralec through pid (player id) as i need to get the population from naselje, and after get the name of the tribe by connecting igralec with pleme using tid (tribe id).. I wrote the following and it works perfectly:
select p.*, sum(n.population) as populacija
from naselje n, igralec i, pleme p
where n.pid = i.pid and i.tid = p.tid
group by tid
order by populacija desc
limit 1;
The only drawback here is the limit 1 query as there might be more than one column with same value… there must be an alternative way to solve this without using LIMIT
Your query will always need to build the entire internal table and sum by group in order to figure the answer to highest population. All the limit does is grab the first item off the final set. What you really probably care about is not having to sort the result. I can’t think of a better way than you have it.