简体   繁体   English

检查表值是否包含在我发送的字符串中的 SQL 语句

[英]SQL statement that checks if a table value is contained within a string I send it

I know about the LIKE operator, but for what im trying to do, I would need to swap the column with the value, which didn't work.我知道 LIKE 运算符,但是对于我尝试做的事情,我需要将列与值交换,这不起作用。

Im using this in a discord bot that is connected to a database with a table.我在连接到带有表的数据库的不和谐机器人中使用它。 The table has two columns, keyword and response.该表有两列,关键字和响应。 I need a query that could give me the response when a given string contains the keyword.当给定的字符串包含关键字时,我需要一个可以给我响应的查询。

SELECT response FROM Reply WHERE (insert something here to see if provided string 
contains table value)

Use of the SQL tag indicates your question relates to standard SQL (hover over the tag and read it).使用SQL标记表示您的问题与标准 SQL 相关(将鼠标悬停在标记上并阅读它)。

LIKE cannot be used for your purpose because the standard is quite clear on what you can specify : LIKE 不能用于您的目的,因为标准非常清楚您可以指定的内容:

<character like predicate> ::=
<row value predicand> <character like predicate part 2>
<character like predicate part 2> ::=
[ NOT ] LIKE <character pattern> [ ESCAPE <escape character> ]

Therefore you can't WHERE 'myliteral' LIKE colname .因此你不能WHERE 'myliteral' LIKE colname

So you'd need a scalar function, but I am not aware of any scalar function defined in the standard that you can use for this purpose.所以你需要一个标量函数,但我不知道标准中定义的任何标量函数可以用于此目的。

So you are confined to scalar functions offered by your particular DBMS.因此,您只能使用特定 DBMS 提供的标量函数。 Eg in DB2, there is POSSTR(source_string, search_string) that you could use as POSSTR('myliteral', colname).例如,在 DB2 中,您可以将 POSSTR(source_string, search_string) 用作 POSSTR('myliteral', colname)。

Select top 1 response
from Reply t
where concat('%',keyword,'%') like '<user provided text>'
--using % let check if the keyword is 'contained' in user's text

PS CONCAT used this way works on SQLSERVER check the correspondence for the dbms you're using, also top1 stops the query once it finds the first keyword PS CONCAT 使用这种方式适用于 SQLSERVER 检查您正在使用的 dbms 的对应关系,一旦找到第一个关键字,top1 也会停止查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM