If I run the following query:
SELECT loc2.* FROM `locations` AS loc INNER JOIN `locations` AS loc2 ON loc.location_id = loc2.location_parent WHERE loc.location_status='publish'
I get the following result:
+-------------+------------------+-----------------+-----------------+ | location_id | location_name | location_parent | location_status | +-------------+------------------+-----------------+-----------------+ | 19 | Dhaka Division | 564 | publish | | 22 | Dhaka District | 19 | publish | | 26 | Dhaka City | 22 | publish | | 28 | Mirpur | 26 | publish | | 30 | Mirpur - 12 | 28 | publish | | 32 | Mirpur DOHS | 30 | publish | | 634 | Gazipur District | 19 | publish | +-------------+------------------+-----------------+-----------------+ 7 rows in set (0.00 sec)
Actually I am trying to get all the children/grand-children from database. Now the above query is just sorting the rows as per the parent-child rule and returning all the rows. But, I want to add a condition to the SQL, so that it gets children only of a specific parent.
For example, I want to get all the child rows/nodes/locations of 19
. And the result set should be following:
+-------------+------------------+-----------------+-----------------+ | location_id | location_name | location_parent | location_status | +-------------+------------------+-----------------+-----------------+ | 22 | Dhaka District | 19 | publish | | 26 | Dhaka City | 22 | publish | | 28 | Mirpur | 26 | publish | | 30 | Mirpur - 12 | 28 | publish | | 32 | Mirpur DOHS | 30 | publish | +-------------+------------------+-----------------+-----------------+
I tried with this:
SELECT loc2.* FROM `locations` AS loc INNER JOIN `locations` AS loc2 ON loc.location_id = loc2.location_parent WHERE loc.location_status='publish' AND loc2.location_parent=19
and this:
SELECT loc2.* FROM `locations` AS loc INNER JOIN `locations` AS loc2 ON loc.location_id = loc2.location_parent WHERE loc.location_status='publish' AND loc.location_parent=19
But they both return the same result, as it should :
+-------------+------------------+-----------------+-----------------+ | location_id | location_name | location_parent | location_status | +-------------+------------------+-----------------+-----------------+ | 22 | Dhaka District | 19 | publish | +-------------+------------------+-----------------+-----------------+
So, what should I do to achieve the result I need?