gpt4 book ai didi

sql - 字符串的 FIND_IN_SET 示例

转载 作者:行者123 更新时间:2023-11-29 14:27:51 25 4
gpt4 key购买 nike

我正在使用 FIND_IN_SET 并收到以下错误

SQL Error [42883]: ERROR: function find_in_set(unknown, character varying[]) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.

我用了下面的例子

where FIND_IN_SET ('US', country_codes) and  
where FIND_IN_SET ('"US"', country_codes)

表格如下

COUNTRY_CODES
{US, CA}
{US, CA, UT}
{FR, BG, ET}

and want to use for group by country_codes where as it should consider COUNTRY_CODES
{US, CA}
{US, CA, UT} as same country as it is coming in the query
where as group by consider it as different country

最佳答案

这个错误输出看起来很像你正在运行 Postgres,不是MySQL。

如果 country_codes 列实际上是一个 Postgres 数组,那么只需使用它:

WHERE country_codes @> ARRAY['US']::varchar[]

如果您确实将 CSV 数据存储在字符串列中,那么我建议改用以下逻辑:

WHERE ',' || country_codes || ',' LIKE '%,US,%'

要查看上述逻辑的作用,假设您正在尝试匹配以下 CSV 国家代码列表:

US,CA,UT

变成

,US,CA,UT,

然后,很明显 ,US, 出现在该 CSV 字符串中。请注意,您应该避免将 CSV 数据直接存储在 SQL 表中。

关于sql - 字符串的 FIND_IN_SET 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55914461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com