I’ve a table with following structure and values:
location_id, location_name, parent_id
1, USA, 0
2, Illinois, 1
3, Chicago, 2
4, New York, 1
In this fragment, then row with 'USA' would be considered a country, because value of parent_id is 0. But, if parent_id has some other value, then it would signify, that the particular location is under some location. We have multiple levels of location and it is not set. It can be 2 level, 3 level, 4 level, etc.
For ex. USA > Alabama > Alabama Gulf Coast > Gulf Shores
I need to get all location_id entries, which has no further location. So that in above example, I should get answer as “Gulf Shores”.
I think the solution should be something like:
SELECT location_id FROM location WHERE "( parent_id does not contain any value from location_id )"
But i cannot figure out the exact syntax. How should I implement this?
1 Answer