0

次のような構造のテーブルがあります。

+-----------------------+------------------+------+-----+---------+----------------+
| 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    |                |
+-----------------------+------------------+------+-----+---------+----------------+

ここで、1つの場所にparent_location_idがある場合とない場合があります。これは、同じテーブルの別の行です。

ここで、少なくとも1つのサブロケーションを持つすべてのロケーションを選択します。次のクエリがあります。それが正しいか?

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 
4

1 に答える 1

0

LEFT JOINの代わりに使用INNER JOIN

SELECT  a.location_id
        a.name, 
        a.is_delivery_available, 
        a.is_booking_available
FROM   `locations` a
            LEFT JOIN  `locations` b 
                ON a.location_id = b.parent_location_id
WHERE a.`city_id` = 5
ORDER BY a.name ASC
于 2012-08-07T04:15:48.517 に答える