PostgreSQL Reverse LIKE

后端 未结 2 1519
梦毁少年i
梦毁少年i 2020-12-22 02:32

I need to test if any part of a column value is in a given string, instead of whether the string is part of a column value. For instance:

This way, I can find if any

2条回答
  •  忘掉有多难
    2020-12-22 03:00

    This query:

    SELECT 
    regexp_split_to_table(
      'The ships hung in the sky in much the same way that bricks don’t', 
      '\s' );
    

    gives a following result:

    | regexp_split_to_table |
    |-----------------------|
    |                   The |
    |                 ships |
    |                  hung |
    |                    in |
    |                   the |
    |                   sky |
    |                    in |
    |                  much |
    |                   the |
    |                  same |
    |                   way |
    |                  that |
    |                bricks |
    |                 don’t |
    

    Now just do a semijoin against a result of this query to get desired results

    SELECT * FROM table t
    WHERE EXISTS (
       SELECT * FROM (
          SELECT 
        regexp_split_to_table(
          'The ships hung in the sky in much the same way that bricks don’t', 
          '\s' ) x
       ) x
       WHERE t.column LIKE '%'|| x.x || '%'
    )  
    

提交回复
热议问题