I have a setup for 2 tables which is rather curious.
Table A:
it has multiple columns, but is basically a collection of information.
Say:
------------------------------------
my_id| name | address | lat | long
Table B is a long list of relationshisp between elements of table A:
For example:
------------------
my_id | parent_id
What I want to do is the following:
For any A.my_id, get the list of the B.my_id which have A.my_id as B.parent_id (there is also the case in which there’s noone) and then:
get, for each element in this list (we just got), lats and longs from table A for these ids and create an 2 array out of them.
Can anyone point me in the right direction? I am rather new to SQL and while I managed up ’til now, this really baffles me.
Thanks!
Ignoring the selected columns for a minute, the query will give you rows from table A that match the particular ID you’re looking for (see the where clause), joined with rows from B that have the same parent ID as the ID you’re looking for, joined with table A again, this time rows that have an ID that matches the ID in B.
If your tables look like this:
If you do this query:
You should get these results:
From there we just change the columns in the
SELECTto affect what the results look like.The trick here is using the same table (A) in two places in the
JOIN.