[英]Conditional Concatenation of SQL Query results
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.如您所见,表的每个条目都有不同的 level_depth。
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.我想将 level_depth=3 的所有条目与其 id_parent 的名称连接起来,并显示所有 level_depth=2 的条目而不进行任何连接。
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:这需要 id_parent 对 level_depth 2 进行左连接。您最终应该得到如下结果:
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.'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.