是否可以将 WHERE IN 与 LIKE 一起使用?

发布于 2024-10-04 13:01:32 字数 1075 浏览 6 评论 0原文

如果我必须搜索一些数据,我可以使用通配符并使用简单的查询 -

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

So尛奶瓶 2024-10-11 13:01:32
SELECT * 
FROM TABLE A
   INNER JOIN AnotherTable B on
     A.COL1 = B.col
WHERE COL1 LIKE '%test_string%'

根据提供的示例代码,尝试一下。最终的 select 语句显示您所请求的数据。

create table #AnotherTable
(
    ID int IDENTITY(1,1) not null primary key,
    Col varchar(100)
);

INSERT INTO #AnotherTable(col) values('one')
INSERT INTO #AnotherTable(col) values('two')
INSERT INTO #AnotherTable(col) values('three')

create table #Table
(
    Col varchar(100),
    Col1 varchar(100)
);

INSERT INTO #Table(Col,Col1) values('aa','one')
INSERT INTO #Table(Col,Col1) values('bb','two')
INSERT INTO #Table(Col,Col1) values('cc','three')
INSERT INTO #Table(Col,Col1) values('dd','four')
INSERT INTO #Table(Col,Col1) values('ee','one_two')
INSERT INTO #Table(Col,Col1) values('ff','three_two')

SELECT * FROM #AnotherTable
SELECT * FROM #Table

SELECT * FROM #Table WHERE COL1 IN(Select col from #AnotherTable)


SELECT distinct A.*
FROM #Table A
    INNER JOIN  #AnotherTable B on
        A.col1 LIKE '%'+B.Col+'%'

DROP TABLE #Table
DROP TABLE #AnotherTable
SELECT * 
FROM TABLE A
   INNER JOIN AnotherTable B on
     A.COL1 = B.col
WHERE COL1 LIKE '%test_string%'

Based on the example code provided, give this a try. The final select statement presents the data as you have requested.

create table #AnotherTable
(
    ID int IDENTITY(1,1) not null primary key,
    Col varchar(100)
);

INSERT INTO #AnotherTable(col) values('one')
INSERT INTO #AnotherTable(col) values('two')
INSERT INTO #AnotherTable(col) values('three')

create table #Table
(
    Col varchar(100),
    Col1 varchar(100)
);

INSERT INTO #Table(Col,Col1) values('aa','one')
INSERT INTO #Table(Col,Col1) values('bb','two')
INSERT INTO #Table(Col,Col1) values('cc','three')
INSERT INTO #Table(Col,Col1) values('dd','four')
INSERT INTO #Table(Col,Col1) values('ee','one_two')
INSERT INTO #Table(Col,Col1) values('ff','three_two')

SELECT * FROM #AnotherTable
SELECT * FROM #Table

SELECT * FROM #Table WHERE COL1 IN(Select col from #AnotherTable)


SELECT distinct A.*
FROM #Table A
    INNER JOIN  #AnotherTable B on
        A.col1 LIKE '%'+B.Col+'%'

DROP TABLE #Table
DROP TABLE #AnotherTable
淡莣 2024-10-11 13:01:32

是的。使用关键字 AND:

SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable) AND COL1 LIKE '%test_string%'

但在这种情况下,您最好使用 JOIN 语法:

SELECT TABLE.* FROM TABLE JOIN AnotherTable on TABLE.COL1 = AnotherTable.col WHERE TABLE.COL1 LIKE '%test_string'

Yes. Use the keyword AND:

SELECT * FROM TABLE WHERE COL1 IN (Select col from AnotherTable) AND COL1 LIKE '%test_string%'

But in this case, you are probably better off using JOIN syntax:

SELECT TABLE.* FROM TABLE JOIN AnotherTable on TABLE.COL1 = AnotherTable.col WHERE TABLE.COL1 LIKE '%test_string'
吃素的狼 2024-10-11 13:01:32

否,因为 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

爱殇璃 2024-10-11 13:01:32

模式匹配运算符是:

  1. IN,针对值列表,
  2. LIKE,针对模式,
  3. REGEXP/RLIKE 针对正则表达式(包括通配符和替代方案,因此最接近“通过一组值使用通配符”,例如 (ab)+a|(ba)+b 将匹配所有字符串 aba...ba 或 bab... ab),
  4. FIND_IN_SET获取集合中字符串的索引(表示为逗号分隔的字符串),
  5. SOUNDS LIKE 根据字符串的发音和
  6. MATCH ... AGAINST 用于全文匹配。

这就是字符串匹配的情况,尽管还有其他字符串函数。

例如,您可以尝试加入 Table.Col1 LIKE CONCAT(AnotherTable.Col, '%'),尽管性能可能会很糟糕(假设它有效)。

The pattern matching operators are:

  1. IN, against a list of values,
  2. LIKE, against a pattern,
  3. 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),
  4. FIND_IN_SET to get the index of a string in a set (which is represented as a comma separated string),
  5. SOUNDS LIKE to compare strings based on how they're pronounced and
  6. MATCH ... 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).

来世叙缘 2024-10-11 13:01:32

尝试交叉联接,以便您可以将 AnotherTable 中的每一行与 Table 中的每一行进行比较:

SELECT DISTINCT t.Col, t.Col1
FROM AnotherTable at
CROSS JOIN Table t 
WHERE t.col1 LIKE ('%' + at.col + '%')

为了安全起见,您需要在 at.col 中转义通配符。尝试 这个答案为此。

Try a cross join, so that you can compare every row in AnotherTable to every row in Table:

SELECT DISTINCT t.Col, t.Col1
FROM AnotherTable at
CROSS JOIN Table t 
WHERE t.col1 LIKE ('%' + at.col + '%')

To make it safe, you'll need to escape wildcards in at.col. Try this answer for that.

柒夜笙歌凉 2024-10-11 13:01:32

如果我正确理解了这个问题,那么当“Table.Col1”位于“AnotherTable.Col”中时,您需要“Table”中的行,并且当 Col1 IS LIKE '%some_string%' 时,您还需要“Table”中的行。

如果是这样,你想要类似的东西:

SELECT 
    t.* 
FROM 
    [Table] t 
LEFT JOIN 
    [AnotherTable] at ON t.Col1 = at.Col 
WHERE (at.Col IS NOT NULL
    OR t.Col1 LIKE '%some_string%')

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:

SELECT 
    t.* 
FROM 
    [Table] t 
LEFT JOIN 
    [AnotherTable] at ON t.Col1 = at.Col 
WHERE (at.Col IS NOT NULL
    OR t.Col1 LIKE '%some_string%')
掩于岁月 2024-10-11 13:01:32

像这样的东西吗?

SELECT * FROM TABLE 
WHERE 
   COL1 IN (Select col from AnotherTable) 
   AND COL1 LIKE '%test_string%'

Something like this?

SELECT * FROM TABLE 
WHERE 
   COL1 IN (Select col from AnotherTable) 
   AND COL1 LIKE '%test_string%'
殊姿 2024-10-11 13:01:32

您是否正在考虑“存在”之类的事情?

SELECT * FROM TABLE t WHERE EXISTS(从 AnotherTable t2 中选择 col,其中 t2.col = t.col like '%test_string%')

Are you thinking about something like EXISTS?

SELECT * FROM TABLE t WHERE EXISTS (Select col from AnotherTable t2 where t2.col = t.col like '%test_string%' )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文