Postgresql Select rows where column = array

后端 未结 4 1902
闹比i
闹比i 2020-12-12 17:37

This is a summary of what I am trying to do:

$array[0] = 1;
$array[1] = 2;

$sql = \"SELECT * FROM table WHERE some_id = $array\"

Obviously

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 18:18

    SELECT  *
    FROM    table
    WHERE   some_id = ANY(ARRAY[1, 2])
    

    or ANSI-compatible:

    SELECT  *
    FROM    table
    WHERE   some_id IN (1, 2)
    

    The ANY syntax is preferred because the array as a whole can be passed in a bound variable:

    SELECT  *
    FROM    table
    WHERE   some_id = ANY(?::INT[])
    

    You would need to pass a string representation of the array: {1,2}

提交回复
热议问题