I have a table with following structure,
+-----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------+----------------+
| location_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(60) | NO | | NULL | |
| city_id | int(10) unsigned | NO | MUL | NULL | |
| parent_location_id | int(11) | NO | | NULL | |
+-----------------------+------------------+------+-----+---------+----------------+
Here one location may or may not have a parent_location_id, which is another row in the same table.
Now I want to select all location which have atleast one sublocation. I have the following query. Is it correct?
SELECT DISTINCT (
a.location_id
), a.name, a.is_delivery_available, a.is_booking_available
FROM `locations` a
JOIN `locations` b ON a.location_id = b.parent_location_id
WHERE a.`city_id` =5
ORDER BY a.name ASC
use
LEFT JOINinstead ofINNER JOIN