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 Server 中单行 MERGE/upsert 的语法 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01