[Operation and Maintenance|Database] How to convert FIND_IN_SET in msql into the function of ARRAY_POSITION in pg data

In MySQL, FIND_IN_SETthe function is used to find whether a value exists in a comma-separated list of strings. In PostgreSQL, you can use string_to_arraythe function to convert a comma-delimited string to an array, and then use ARRAY_POSITIONthe function to find whether a value is in the array.

Here's how to convert a function MySQLin FIND_IN_SETto PostgreSQLa function in :

MySQL query example:

SELECT FIND_IN_SET('value', 'value1,value2,value3');

Equivalent PostgreSQL query example:

SELECT CASE WHEN ARRAY_POSITION(string_to_array('value1,value2,value3', ','), 'value') IS NOT NULL THEN 'Found' ELSE 'Not Found' END;

In the above PostgreSQLquery, we first use string_to_arraythe function to convert the comma separated string 'value1,value2,value3' into an array, and then use ARRAY_POSITIONthe function to find whether 'value' is in the array. If the function returns a position other than NULL, then the value was found.

Guess you like

Origin blog.csdn.net/macaiyun0629/article/details/132914904