- Автор темы
- #1
есть таблица
нужно получить данные в следующем формате
данного результата добился следующим запросом,
но походу так делать полный маразм
структура таблицы для тестов
Код:
mysql> select * from location_copy;
+----+-----------+----------+-----------+
| id | name | id_rajon | id_oblast |
+----+-----------+----------+-----------+
| 1 | olast_1 | NULL | NULL |
| 2 | oblast_2 | NULL | NULL |
| 3 | rajon_1 | NULL | 1 |
| 4 | rajon_2 | NULL | 2 |
| 5 | poselok_1 | 3 | 1 |
| 6 | poselok_2 | 4 | 1 |
+----+-----------+----------+-----------+
Код:
+----+-----------+---------+----------+
| id | поселок | раен | область |
+----+-----------+---------+----------+
| 1 | NULL | NULL | olast_1 |
| 2 | NULL | NULL | oblast_2 |
| 3 | NULL | rajon_1 | olast_1 |
| 4 | NULL | rajon_2 | oblast_2 |
| 5 | poselok_1 | rajon_1 | olast_1 |
| 6 | poselok_2 | rajon_2 | olast_1 |
+----+-----------+---------+----------+
но походу так делать полный маразм
PHP:
SELECT
t1.id,
null as 'поселок',
null as 'раен',
t1.name as 'область'
FROM location_copy as t1
WHERE (t1.id_rajon IS NULL
AND t1.id_oblast IS NULL)
UNION
SELECT
t1.id,
null as 'поселок',
t1.name as 'раен',
t2.name as 'область'
FROM location_copy as t1
LEFT JOIN location_copy as t2
ON t1.id_oblast = t2.id
WHERE t1.id_rajon IS NULL
AND t1.id_oblast IS NOT NULL
UNION
SELECT
t1.id,
t1.name as 'поселок',
t2.name as 'раен',
t3.name as 'область'
FROM location_copy as t1
LEFT JOIN location_copy as t2
ON t1.id_rajon = t2.id
LEFT JOIN location_copy as t3
ON t1.id_oblast = t3.id
WHERE (t1.id_rajon and t1.id_oblast ) is not NULL;
PHP:
CREATE TABLE location_copy(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL COMMENT 'название',
id_rajon INT(11) DEFAULT NULL COMMENT 'район',
id_oblast INT(11) DEFAULT NULL COMMENT 'область',
PRIMARY KEY (id),
INDEX FK_location_rajon_id2 (id_rajon),
INDEX FK_location_region_id (id_oblast)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci;
INSERT INTO location_copy VALUES
(1, 'olast_1', NULL, NULL),
(2, 'oblast_2', NULL, NULL),
(3, 'rajon_1', NULL, 1),
(4, 'rajon_2', NULL, 2),
(5, 'poselok_1', 3, 1),
(6, 'poselok_2', 4, 1);