mysql in list only validates first id in list. maybe a blob issue(列表中的 mysql 仅验证列表中的第一个 id.也许是斑点问题)
问题描述
我使用的系统使用逗号分隔值作为一对二的关系.它以 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.也许是斑点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:列表中的 mysql 仅验证列表中的第一个 id.也许是斑点问题
基础教程推荐
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
