I'd like to show the results of a query concatenated with their self based on a value.
I have this query
SELECT CONCAT(cl.`name`,\',\') as name
FROM `'._DB_PREFIX_.'category_lang` AS cl
INNER JOIN `'._DB_PREFIX_.'category` AS c ON (cl.`id_category` = c.`id_category`)
WHERE cl.`name` LIKE \'%'.pSQL($searchData).'%\'
AND c.`level_depth` NOT IN (0, 1, 4, 5) and cl.`id_lang`='.(int)$context->language->id.'
which shows the list of available location for a booking platform, based on what the user is typing.
This is the results list:
Lisbon
Ortisei
Palermo
Polignano a Mare
Portugal
The inner join table results is the following
as you can see there are different level_depth for each entry of the table.
I would like to CONCAT all the entries with level_depth=3 with the name of their id_parent and showing all the entries with level_depth=2 without any concatenation.
The above results should become:
Lisbon, Portugal
Ortisei, Italy
Palermo, Italy
Polignano a Mare, Italy
Portugal
This requires a left join by id_parent for level_depth 2. You should end up with something like this:
SELECT
CONCAT_WS(\', \', cl.`name`, cl_parent.`name`) as name
FROM `'._DB_PREFIX_.'category_lang` AS cl
INNER JOIN `'._DB_PREFIX_.'category` AS c
ON (cl.`id_category` = c.`id_category`)
LEFT JOIN `'._DB_PREFIX_.'category` AS c_parent
ON (c_parent.`id_category` = c.`id_parent`)
AND c_parent.`level_depth` = 2
LEFT JOIN `'._DB_PREFIX_.'category_lang` AS cl_parent
ON (cl_parent.`id_category` = c_parent.`id_category`)
AND cl_parent.id_lang = cl.id_lang
WHERE
CONCAT_WS(\', \', cl.`name`, cl_parent.`name`) LIKE \'%'.pSQL($searchData).'%\'
AND c.`level_depth` IN (2, 3) and cl.`id_lang`='.(int)$context->language->id.'