[英]MySQL: Using a string in a "WHERE <column_name> IN <values>" Query
Dear Stackoverflow members,亲爱的 Stackoverflow 会员,
I hope that I'm not overlooking something obvious, but I haven't been able to find a solution to my problem so far.我希望我没有忽略一些明显的东西,但到目前为止我还没有找到解决问题的方法。
I need to obtain certain column values from a table, using a standard WHERE... IN ( 1, 2, 3, 4 )
clause.我需要使用标准WHERE... IN ( 1, 2, 3, 4 )
子句从表中获取某些列值。 The problem, however, is that the values in the WHERE clause have to be built dynamically, based on values stored in columns of another table, that look something like 'a:3:{i:0;s:3:"153";i:1;s:3:"211";i:2;s:3:"226";}'
.然而,问题是 WHERE 子句中的值必须动态构建,基于存储在另一个表的列中的值,类似于'a:3:{i:0;s:3:"153";i:1;s:3:"211";i:2;s:3:"226";}'
。 The values to be extracted are, in this example, 153, 211, and 226 resulting in the clause WHERE <column> IN ( 153, 211, 226 )
.在此示例中,要提取的值是 153、211 和 226,这导致子句WHERE <column> IN ( 153, 211, 226 )
。 The formatting of this string is always the same, but the number of values can vary.此字符串的格式始终相同,但值的数量可能会有所不同。
I can obtain the actual values from this string, using some RegExp trickery with我可以从这个字符串中获取实际值,使用一些 RegExp 技巧
SELECT value from `column` WHERE <non-scary WHERE clause> INTO @valueString;
SET @IDs = CONVERT(PREG_REPLACE('/[^\"]+\"([^\"]+)\".*/', '$1', PREG_REPLACE( '/\"\;[^\"]+\"/', '\,\ ', @valueString )) USING UTF8);
The function PREG_REPLACE is a UDF, from [http://www.mysqludf.org/lib_mysqludf_preg/], which is the one generally recommended, whenever people are looking for RegExp/Replace functions. function PREG_REPLACE 是一个 UDF,来自 [http://www.mysqludf.org/lib_mysqludf_preg/],这是人们在寻找 RegExp/Replace 函数时普遍推荐的一个。 The CONVERT()
is there, because the PREG_REPLACE gives me back a blob, instead of a readable string. CONVERT()
在那里,因为 PREG_REPLACE 给我返回一个 blob,而不是一个可读的字符串。
After running these queries, running运行这些查询后,运行
SELECT @IDs;
gives me a list of values, looking like (in this example) 153, 211, 226
.给我一个值列表,看起来像(在这个例子中) 153, 211, 226
。
The question is now, how I can convince MySQL to use these values in the WHERE... IN
clause.现在的问题是,我如何说服 MySQL 在WHERE... IN
子句中使用这些值。 The naive query天真的查询
SELECT * FROM `table` WHERE id IN ( SELECT @IDs );
does not complain as such, but only gives back the result for the first of the values in the list, ignoring the other ones (I have to admit, that I don't really understand why it does that).不会这样抱怨,而只会返回列表中第一个值的结果,而忽略其他值(我不得不承认,我真的不明白它为什么这样做)。
Quite obviously, this would all be trivial to do in eg, Ruby, Python, Perl, etc. but for technical reasons, it has to be done strictly in SQL. One of the suggestions a colleague gave me, is to use a temporary table, to write the intermediate results, but I'm not sure if I see how that'd be implemented and/or work.很明显,这在 Ruby、Python、Perl 等中都是微不足道的,但出于技术原因,必须严格在 SQL 中完成。一位同事给我的建议之一是使用临时表, 来写中间结果,但我不确定我是否看到它是如何实现和/或工作的。 Moreover, I don't think it can be done easily without lots of loops, substring stuff and other ugliness, but I stand to be corrected.此外,我不认为如果没有很多循环、substring 东西和其他丑陋的东西,它就不会轻易完成,但我会得到纠正。
I hope the description of my problem is more or less clear.我希望我的问题的描述或多或少是清楚的。 I'd appreciate any insights, ideas and hints.我将不胜感激任何见解、想法和提示。
Thanks in advance for your time and mindshare, Constantijn Blondel, Leipzig DE提前感谢您的时间和思想共享,Constantijn Blondel,Leipzig DE
Select a sub query in your where clause it will work ok. Select 您的 where 子句中的子查询可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.