SQL 中的动态 Like 语句

发布于 2024-10-10 10:35:47 字数 353 浏览 5 评论 0原文

我已经绞尽脑汁思考如何做到这一点有一段时间了,我知道这个网站上的一些天才会找到答案。基本上我正在尝试这样做:

SELECT column 
  FROM table 
 WHERE [table].[column] LIKE string1 
       OR [table].[column] LIKE string2 
       OR [table].[column] LIKE string3...

获取存储在表的列中的搜索字符串列表。显然我不能手动为每个字符串执行类似的语句,因为我希望表是动态的。

任何建议都会很棒。 :D

编辑:

我正在使用 MSSQL :(

I've been racking my brain on how to do this for a while, and i know that some genius on this site will have the answer. Basically i'm trying to do this:

SELECT column 
  FROM table 
 WHERE [table].[column] LIKE string1 
       OR [table].[column] LIKE string2 
       OR [table].[column] LIKE string3...

for a list of search strings stored in a column of a table. Obviously I can't do a like statement for each string by hand because i want the table to be dynamic.

Any suggestions would be great. :D

EDIT:

I'm using MSSQL :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一曲琵琶半遮面シ 2024-10-17 10:35:47

将参数(string1、string2、string3...)放入表 (Params),然后使用 LIKE JOIN 到表中>JOIN 子句例如

SELECT column 
  FROM table AS T1
       INNER JOIN Params AS P1
          ON T1.column LIKE '%' + P1.param + '%';

Put the parameters (string1, string2, string3...) into a table (Params) then JOIN to the table using LIKE the JOIN clause e.g.

SELECT column 
  FROM table AS T1
       INNER JOIN Params AS P1
          ON T1.column LIKE '%' + P1.param + '%';
生生不灭 2024-10-17 10:35:47

制作一个示例表_1:

id  Name
1   Fred
2   Joe
3   Frederick
4   Joseph
5   Kenneth

要查找您编码的所有 Freds 和 Jos,

SELECT 
    *
FROM
    Table_1
WHERE 
    name like 'Fred%' OR
    name like 'Jo%'

您想要的是动态 WHERE。您可以通过将通配符放入 Table_2:

id  Search
1   Fred%
2   Jo%

执行 LIKE来实现此目的:

SELECT
    *
FROM
    Table_1 INNER JOIN Table_2
        ON Table_1.name LIKE Table_2.search

并使用 INNER JOIN:结果

id  Name    id  Search
1   Fred    1   Fred%
3   Frederick   1   Fred%
2   Joe 2   Jo%
4   Joseph  2   Jo%

Make a sample Table_1:

id  Name
1   Fred
2   Joe
3   Frederick
4   Joseph
5   Kenneth

To find all the Freds and Jos you code

SELECT 
    *
FROM
    Table_1
WHERE 
    name like 'Fred%' OR
    name like 'Jo%'

What you'd like is a dynamic WHERE. You can achieve this by putting the wildcards in Table_2:

id  Search
1   Fred%
2   Jo%

and performing the LIKE with an INNER JOIN:

SELECT
    *
FROM
    Table_1 INNER JOIN Table_2
        ON Table_1.name LIKE Table_2.search

Result:

id  Name    id  Search
1   Fred    1   Fred%
3   Frederick   1   Fred%
2   Joe 2   Jo%
4   Joseph  2   Jo%
烏雲後面有陽光 2024-10-17 10:35:47

听起来您正在尝试进行全文搜索。 MySQL 有一个用于此目的的工具:
http://dev.mysql.com/doc/refman/5.0 /en/fulltext-boolean.html

SELECT column FROM table MATCH (column) AGAINST ('string1 string2 string3' IN BOOLEAN MODE)

对于 MSSQL,可在以下位置获取一些信息
http://msdn.microsoft.com/en-us/library/cc879300.aspx

SELECT column FROM table WHERE CONTAINS (column , 'string1 string2 string3');

Sounds like you are trying to do a fulltext search. MySQL has a tool for this:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html

SELECT column FROM table MATCH (column) AGAINST ('string1 string2 string3' IN BOOLEAN MODE)

In case of MSSQL some information is available at
http://msdn.microsoft.com/en-us/library/cc879300.aspx

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