当我使用MySQL LIKE
搜索多个字段时,有没有办法知道字符串被找到的字段?
SELECT * FROM DB WHERE title like "%search%" OR description LIKE "%search%" or tags LIKE "%search%
我知道CASE
WHEN
:
SELECT *,CASE
WHEN title like "%search%" THEN 'title'
[...]
END AS found_in
WHERE [...]
但是,如果在多个字段中找到字符串,我最终会喜欢以逗号分隔的字段列表。
这可能吗?
编辑以澄清:
我喜欢我的领域`found_in'成为"标题,描述"当在标题&中找到搜索词时描述。
答案 0 :(得分:1)
一种可能的方式是一系列UNIONed查询,每个字段一个。每个都返回它所在的字段的名称,然后将其用作子查询并对结果执行GROUP_CONCAT: -
SELECT id,
title,
description,
tags,
GROUP_CONCAT(field_found)
FROM
(
SELECT 'title' AS field_found,
id,
title,
description,
tags
FROM DB
WHERE title like "%search%"
UNION
SELECT 'description' AS field_found,
id,
title,
description,
tags
FROM DB
WHERE description LIKE "%search%"
UNION
SELECT 'tags' AS field_found,
id,
title,
description,
tags
FROM DB
WHERE tags LIKE "%search%
) sub0
GROUP BY id,
title,
description,
tags
答案 1 :(得分:0)
proc append data=new base=old;
run;