mysql passing data to in(mysql将数据传递给in)
问题描述
基本上我想从 tblvw1 获取一些数据.另一个表包含列中可能的 ID,该列存储为连接字符串,例如:1|2|3|4".接下来我尝试通过以下查询获得结果:
Basically I want to fetch some data from tblvw1. Another table contains possible ids within a column which is stored as concatenated string, for example: "1|2|3|4". Next I have tried to get the result by the following query:
SELECT x FROM tblname1
WHERE id IN (SELECT REPLACE(content,'|',',') FROM tblname2 WHERE dataid = y )
作为结果,我只得到第一个值,没有获取其他数据.我想子查询会产生这种形式:
AS result I only get the first value, the other data wasn't fetched. I suppose that the subquery will result in this form:
SELECT x FROM tblname1 WHERE id IN ('1,2,3,4')
但我想将这个子查询作为
but i want to have this subquery either as
SELECT x FROM tblname1 WHERE id IN (1,2,3,4)
或
SELECT x FROM tblname1 WHERE id IN ('1','2','3','4')
有什么想法吗?
推荐答案
可以使用find_in_set
函数吗?
SELECT x FROM tblname1 t1
inner join tblname2 t2 on find_in_set (t1.id, REPLACE(t2.content,'|',',')) > 0
where t2.dataid = 'y';
find_in_set
函数返回第一个参数在第二个参数中的位置.如果结果>0,则已找到第一个参数.
The find_in_set
function returns the position of the first argument within the second argument. If the result is >0, then the first argument has been found.
参见 http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set
这篇关于mysql将数据传递给in的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysql将数据传递给in


- 更改自动增量起始编号? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01