PL / PGSQL Dynamic

时间:2016-11-22 20:35:38

标签: postgresql

我需要在PL / PGSQL函数中生成动态Where,例如: 我有这个字符串

  

你好,postgresql,stackoverflow

我需要执行此查询:

SELECT * FROM tblname WHERE tbl_col LIKE %hello% OR tbl_col LIKE %postgresql% OR tbl_col LIKE %stackoverflow%;

有没有办法用逗号分割字符串并生成动态查询?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用SIMILAR TO运算符而不是LIKE。你可以找到文档here

一个例子:

SELECT
    *
FROM
    tblname
WHERE
    tbl_col SIMILAR TO '%(' || REPLACE( 'hello,postgresql,stackoverflow', ',', '|' )|| ')%'

答案 1 :(得分:0)

使用any(array[...])

select * 
from tblname 
where tbl_col like any(array['%hello%', '%postgresql%', '%stackoverflow%']);
相关问题