使用 包含如何搜索多个值

发布于 2025-01-11 02:58:46 字数 456 浏览 2 评论 0原文

这就是我想做的:

select * from Person where CONTAINS((fName,sName),'John AND Doe')

所以我试图搜索 John Doe,但我发现我不能在这里使用 AND (​​只是显示我的思路)那么我如何在 fName 中搜索 John 并在 sName 中搜索 Doe ?但我不想像这样使用“包含”两次:

SELECT * FROM Person 
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');

因为我们可以有

(fName,sName)

但我不能使用

“约翰”、“多伊”/“约翰”和“多伊”

Here is what I am trying to do:

select * from Person where CONTAINS((fName,sName),'John AND Doe')

So I am trying to search for John Doe but I get that I cannot use AND here (just showing my chain of thought) so how can I search for John in the fName and Doe in the sName ? But I don't want to use "Contains" twice like this:

SELECT * FROM Person 
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');

Since we can have

(fName,sName)

but I cannot use

'John','Doe'/'John' AND 'Doe'

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

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

发布评论

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

评论(2

仅此而已 2025-01-18 02:58:46

您的语句

SELECT * FROM Person 
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');

无法编译,因为 CONTAINS 返回一个数字,而不是布尔值。

在oracle上,它应该响应这种错误

ORA-00920: opérateur relationnel non valide
00920. 00000 -  "invalid relational operator"

(关系运算符错误的原因不是AND的存在,而是因为你尝试AND两个数字)

什么你打算做什么?如果要选择 person 表中 fName 列包含子字符串 JohnsName 列包含子字符串 Doe 的行,您可以使用 like 运算符,它使用 % 作为通配符。

SELECT * FROM Person
WHERE fName LIKE '%John%'
and sName LIKE '%Doe%'

我不太练习 CONTAINS 方法,但如果您想使用它,根据文档,您应该使用类似的内容

SELECT * FROM Person
WHERE CONTAINS(fName, 'John') > 0
and CONTAINS(sName,'Doe') > 0

如果您真的不想使用 AND 运算符(对于任何晦涩难懂的内容)原因(例如证明 sql 注入过滤器不好),您可以使用此技巧:比较 2 列和 like 运算符的串联,如下所示,

SELECT * FROM Person 
WHERE fName || sName like '%John%Doe%';

但最后一个技巧也将匹配 所在的行fnameJohn DoesNameJean-Michel :)

Your statement

SELECT * FROM Person 
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');

can't compile because CONTAINS returns a number, not a boolean.

It shall respond,on oracle, with this kind of error

ORA-00920: opérateur relationnel non valide
00920. 00000 -  "invalid relational operator"

(the relational operator error's cause is not the AND's existence, it's because you try to AND two numbers)

What do you intend to do? If you want to select the line in the person table whose fName columns contains the substring John and whose sName column contains the substring Doe, you may use the like operator, which uses % as a wildcard.

SELECT * FROM Person
WHERE fName LIKE '%John%'
and sName LIKE '%Doe%'

I don't practice much the CONTAINS method, but if you desire to use it, according to documentation, you should use something like

SELECT * FROM Person
WHERE CONTAINS(fName, 'John') > 0
and CONTAINS(sName,'Doe') > 0

If you really don't want to use the AND operator (for whatever obscure reason like proving a sql injection filter is bad), you can use this trick : compare a concatenation of the 2 columns and the like operator like so

SELECT * FROM Person 
WHERE fName || sName like '%John%Doe%';

but this last trick will also match the line where fname is John Doe and sName is Jean-Michel :)

甜警司 2025-01-18 02:58:46

这应该有效:

SELECT * FROM dbo.Person
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');

但在您的情况下,您不想使用 2 contains so:

或者,您可以添加一个带有全文索引的新计算列。

添加这样的列:

computedCol AS fName+ ' ' + sName 

并创建全文索引:

CREATE FULLTEXT INDEX ON Person(computedCol LANGUAGE 1033)
KEY INDEX pk_Person_yourPrimaryKeyName

然后您可以执行以下操作:

SELECT * FROM dbo.Person
WHERE CONTAINS(*, 'John AND Doe')

希望它有效。

This should work:

SELECT * FROM dbo.Person
WHERE CONTAINS((fName), 'John')
AND CONTAINS((sName), 'Doe');

But in your case you dont want to use 2 contains so:

Alternatively, you could add a new computed column with a full text index on it.

Add a column like this:

computedCol AS fName+ ' ' + sName 

And create the full text index:

CREATE FULLTEXT INDEX ON Person(computedCol LANGUAGE 1033)
KEY INDEX pk_Person_yourPrimaryKeyName

Then you can do this:

SELECT * FROM dbo.Person
WHERE CONTAINS(*, 'John AND Doe')

Hope it works.

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