I do have a table with locations like this:
ID | NAME | ADDRESS | PARENT_ID
1 | name A | address A | NULL
2 | name B | NULL | 1
Locations with a parent location do not always have an address (in case it's the same as the parent).
CREATE TABLE `location2` (
`id` int(5) NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`id_parent_location` int(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `location2` (`id`, `name`, `address`, `id_parent_location`) VALUES
(1, 'NAME A', 'ADDRESS A', NULL),
(2, 'NAME B', NULL, 1);
Anyone who can help me? Thanks!
I've tried the following:
SELECT CASE
WHEN `address` IS NULL THEN (SELECT `address` FROM `location` WHERE `id` = `id_parent_location`)
ELSE `address`
END
FROM `location` WHERE `id` = 2
So in case of id=2 I want to show "name B" and "address A".