是否可以将 WHERE IN 与 LIKE 一起使用?
如果我必须搜索一些数据,我可以使用通配符并使用简单的查询 -
SELECT * FROM TABLE WHERE COL1 LIKE '%test_string%'
而且,如果我必须查看我可以使用的许多值 -
SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable)
但是,是否可以一起使用两者。也就是说,查询不仅执行 WHERE IN,还执行类似于 WHERE LIKE? 的操作。不查看一组值而是使用通配符搜索一组值的查询。
如果这还不清楚我可以举个例子。让我知道。谢谢。
示例 -
让我们考虑 -
AnotherTable -
id | Col
------|------
1 | one
2 | two
3 | three
表 -
Col | Col1
------|------
aa | one
bb | two
cc | three
dd | four
ee | one_two
bb | three_two
现在,如果我可以使用
SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable)
这给了我 -
Col | Col1
------|------
aa | one
bb | two
cc | three
但如果我需要怎么办 -
Col | Col1
------|------
aa | one
bb | two
cc | three
ee | one_two
bb | three_two
我想这应该可以帮助您理解我将 WHERE IN 和 LIKE 一起使用的意思
If I have to search for some data I can use wildcards and use a simple query -
SELECT * FROM TABLE WHERE COL1 LIKE '%test_string%'
And, if I have to look through many values I can use -
SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable)
But, is it possible to use both together. That is, the query doesn't just perform a WHERE IN but also perform something similar to WHERE LIKE? A query that just doesn't look through a set of values but search using wildcards through a set of values.
If this isn't clear I can give an example. Let me know. Thanks.
Example -
lets consider -
AnotherTable -
id | Col
------|------
1 | one
2 | two
3 | three
Table -
Col | Col1
------|------
aa | one
bb | two
cc | three
dd | four
ee | one_two
bb | three_two
Now, if I can use
SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable)
This gives me -
Col | Col1
------|------
aa | one
bb | two
cc | three
But what if I need -
Col | Col1
------|------
aa | one
bb | two
cc | three
ee | one_two
bb | three_two
I guess this should help you understand what I mean by using WHERE IN and LIKE together
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
根据提供的示例代码,尝试一下。最终的 select 语句显示您所请求的数据。
Based on the example code provided, give this a try. The final select statement presents the data as you have requested.
是的。使用关键字 AND:
但在这种情况下,您最好使用 JOIN 语法:
Yes. Use the keyword AND:
But in this case, you are probably better off using JOIN syntax:
否,因为 LIKE 子句中的每个元素都需要通配符,而 IN 子句无法做到这一点
no because each element in the LIKE clause needs the wildcard and there's not a way to do that with the IN clause
模式匹配运算符是:
IN
,针对值列表,LIKE
,针对模式,REGEXP
/RLIKE
针对正则表达式(包括通配符和替代方案,因此最接近“通过一组值使用通配符”,例如(ab)+a|(ba)+b
将匹配所有字符串 aba...ba 或 bab... ab),FIND_IN_SET
获取集合中字符串的索引(表示为逗号分隔的字符串),
SOUNDS LIKE
根据字符串的发音和MATCH ... AGAINST
用于全文匹配。这就是字符串匹配的情况,尽管还有其他字符串函数。
例如,您可以尝试加入
Table.Col1 LIKE CONCAT(AnotherTable.Col, '%')
,尽管性能可能会很糟糕(假设它有效)。The pattern matching operators are:
IN
, against a list of values,LIKE
, against a pattern,REGEXP
/RLIKE
against a regular expression (which includes both wildcards and alternatives, and is thus closest to "using wildcards through a set of valuws", e.g.(ab)+a|(ba)+b
will match all strings aba...ba or bab...ab),FIND_IN_SET
to get the index of a string in a set (which is represented as a comma separated string),SOUNDS LIKE
to compare strings based on how they're pronounced andMATCH ... AGAINST
for full-text matching.That's about it for string matching, though there are other string functions.
For the example, you could try joining on
Table.Col1 LIKE CONCAT(AnotherTable.Col, '%')
, though performance will probably be dreadful (assuming it works).尝试交叉联接,以便您可以将 AnotherTable 中的每一行与 Table 中的每一行进行比较:
为了安全起见,您需要在 at.col 中转义通配符。尝试 这个答案为此。
Try a cross join, so that you can compare every row in AnotherTable to every row in Table:
To make it safe, you'll need to escape wildcards in at.col. Try this answer for that.
如果我正确理解了这个问题,那么当“Table.Col1”位于“AnotherTable.Col”中时,您需要“Table”中的行,并且当 Col1 IS LIKE '%some_string%' 时,您还需要“Table”中的行。
如果是这样,你想要类似的东西:
If I understand the question correctly you want the rows from "Table" when "Table.Col1" is IN "AnotherTable.Col" and you also want the rows when Col1 IS LIKE '%some_string%'.
If so you want something like:
像这样的东西吗?
Something like this?
您是否正在考虑“存在”之类的事情?
Are you thinking about something like EXISTS?