问题描述
我使用的系统使用逗号分隔值作为一对二的关系.它以 blob 形式存储.
I work with a system that used comma separated values as a one-2-many relation. It is stored in as a blob.
我尝试使用 MySQL IN (LIST),但最终只得到 uid 在列表中第一个或只有一个在列表中的行.
I try to use the MySQL IN (LIST), but only ends up with the rows where the uid is first in the list or only one in the list.
mytable
uid categories
1 24,25,26
2 25
3 22,23,25
4 25,27
run sql:
SELECT * FROM mytable WHERE 25 IN (categories)
Result:
uid categories
2 25
4 25,27
Missing (should have been selected but are not)
uid categories
1 24,25,26
3 22,23,25
知道为什么字符串必须以 25 开头而不是只包含 25 吗?我想这是一个字符串问题而不是 IN (LIST) 问题
Any idea why a string has to start with 25 and not just contain 25? I guess it is a string issue rather than an IN (LIST) issue
更新 - 基于以下答案:
在 blob 上使用 th IN (LIST) 时,blob 将转换为 int 并且逗号和数字"丢失.
When using th IN (LIST) on a blob, the blob is converted to an int and commas and "digits" are lost.
改为使用 FIND_IN_SET(needle, haystack),它也适用于包含逗号分隔值的 blob.
In stead use FIND_IN_SET(needle, haystack) that will work also on a blob containing comma separated values.
推荐答案
我认为您正在寻找 FIND_IN_SET
SELECT * FROM mytable WHERE FIND_IN_SET(25,category)
这篇关于列表中的 mysql 仅验证列表中的第一个 id.也许是斑点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!