gpt4 book ai didi

mysql - 检查单元格中是否存在国家代码

转载 作者:行者123 更新时间:2023-11-29 01:41:43 24 4
gpt4 key购买 nike

在用户向数据库添加任何内容之前,我正在尝试检查单元格 country 中是否存在国家/地区代码。因此,如果国家代码不存在,他将无法添加任何内容。

我将国家代码存储为逗号分隔值。列类型为 VARCHAR。

"id"    "rtype" "country"
"1" "0" "US,UK,SE,CA"
"2" "1" "US"
"3" "2" "UK"

我运行了以下查询,但结果没有任何行。这里哪里出了问题?问题是,此查询需要使用逗号分隔值以及单个值运行。

select id, rtype from test where id=1 and country in('US')

预期结果

id | rType
1 | 0

select id, rtype from test where id=2 and country in('US')

预期结果

id | rType
2 | 0

最佳答案

试试这个:

SELECT id, rtype FROM test
WHERE FIND_IN_SET('US', country) > 0

这就是 FIND_IN_SET(str,strlist) 函数的工作原理:

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. [...] Returns 0 if str is not in strlist or if strlist is the empty string.

关于mysql - 检查单元格中是否存在国家代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19395205/

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