I have a little problem right here…
So all in all I have exactly two tables:
* resources (ID, resourcetype_ID, chunk_ID, position)
* chunks (ID, x, y)
The first one stores some rows which belongs to the certain chunk.
The second one just stores all the chunks with a different set of x and y as some kind of position.
My goal is to select all the resources that belong to a certain range of chunks.
But I have made a mistake somewhere. I always get the wrong x and y positions, when I select the resources.
SELECT resources.*, chunks.x, chunks.y
FROM resources, chunks
where chunk_ID in (SELECT ID
FROM chunks
where x between 17185-1 and 17185+1
and y between 10846-1 and 10846+1)
group by ID
This query above returns all the resources that belongs to the different chunks. But the returned x and y position is for all of them the same. Why does this happen?
(As you can see the resources with "chunk_ID" = 1 have the same x and y as the resources with "chunk_ID" = 5 … even if those chunks have completely different x and y pairs…)
Normally all resources should have different x and y values regarding to the chunk they belong to.
Best Answer
Your query:
I see several issues:
The two tables in the
FROM
clause are not joined (that's the reason for the irrelevant results you get, the two tables are correlated through the subquery* but theFROM
clause introduces an additional non-correlatedchunks
reference which produces all these unrelated results):Join them:
Since
chunks
is in theFROM
clause, we don't need* the subquery no more, to checkx
andy
:Simplify
WHERE
:You have
SELECT *
combined withGROUP BY
. This is a recipe for spurious, wrong results.Just remove the
GROUP BY
.The query becomes after using all the above comments (and some polishing with table aliases):
* Just for completeness, we could have kept the
IN
subquery and removechunks
from theFROM
clause.This would have the restriction that we couldn't have any column from
chunks
in theSELECT
list - and more likely worse performance than theJOIN
query: