Postgresql:是否可以判断在哪一列中找到了搜索字符串?
Postgresql: is it possible to tell in which column the search string was found?
如何判断在哪一列找到了搜索字符串?
我的查询
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'])
您可以复制 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
. . .
如果您不介意重复条件:
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;
派生的 table(又名 sub-select)是必要的,因为 DISTINCT 对 SELECT 列表的所有表达式进行操作,添加标志可能会改变结果.
如何判断在哪一列找到了搜索字符串?
我的查询
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'])
您可以复制 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
. . .
如果您不介意重复条件:
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;
派生的 table(又名 sub-select)是必要的,因为 DISTINCT 对 SELECT 列表的所有表达式进行操作,添加标志可能会改变结果.