如何分辨在哪一列中找到了搜索字符串?
我的查询
select DISTINCT t1.id,
t2.position ,
t3.name ,
t4 age
FROM table1 AS t1
LEFT JOIN table2 AS t2 on t1.id = t2.fk_id
LEFT JOIN table3 AS t3 on t3.fk_id = t2.fk_id
LEFT JOIN table4 AS t4 on t4.fk_id = t3.fk_id
WHERE
t2.position like ANY(['Real Estate Agent ','25'])
OR
t3.name like ANY(['Real Estate Agent ','25'])
OR
t4 age like ANY(['Real Estate Agent ','25'])
答案 0 :(得分:1)
您将在select
中复制条件:
select . . .,
((case when t2.position like ANY(['Real Estate Agent ','25']) then 'position;' else '' end) ||
(case when t2.name like ANY(['Real Estate Agent ','25']) then 'name;' else '' end) ||
(case when t2.age like ANY(['Real Estate Agent ','25']) then 'age;' else '' end)
) as matching_columns
. . .
答案 1 :(得分:1)
如果您不介意重复这些条件:
select x.*,
position like ANY(['Real Estate Agent ','25']) as found_in_position,
name like ANY(['Real Estate Agent ','25']) as found_in_name,
age like ANY(['Real Estate Agent ','25']) as found_in_age
from (
select DISTINCT
t1.id,
t2.position,
t3.name,
t4.age
FROM table1 AS t1
LEFT JOIN table2 AS t2 on t1.id = t2.fk_id
LEFT JOIN table3 AS t3 on t3.fk_id = t2.fk_id
LEFT JOIN table4 AS t4 on t4.fk_id = t3.fk_id
WHERE
t2.position like ANY(['Real Estate Agent ','25'])
OR
t3.name like ANY(['Real Estate Agent ','25'])
OR
t4.age like ANY(['Real Estate Agent ','25'])
) x;
派生表(又名子选择)是必需的,因为DISTINCT对SELECT列表的所有表达式进行操作,并且添加标志可能会改变结果。