“IN”的等价物使用 AND 而不是 OR 逻辑?
我知道我在这里使用动态 SQL 违反了一些规则,但我仍然需要询问。我继承了一个表,其中包含我需要从中提取记录的每张票证的一系列标签。
简单的例子......我有一个包含“'Apples','Oranges','Grapes'”的数组,我试图检索包含数组中包含的ALL项目的所有记录。
我的 SQL 看起来像这样:
SELECT * FROM table WHERE basket IN ( " + fruitArray + " )
这当然相当于:
SELECT * FROM table WHERE basket = 'Apples' OR basket = 'Oranges' OR basket = 'Grapes'
我很好奇是否有一个函数与 IN ( array ) 的工作方式相同,只是它使用 AND 而不是 OR,以便我可以获得与以下相同的结果:
SELECT * FROM table WHERE basket LIKE '%Apples%' AND basket LIKE '%Oranges%' AND basket LIKE '%Grapes%'
我可能可以手动生成整个字符串,但如果可能的话,希望有一个更优雅的解决方案。任何帮助将不胜感激。
I know I'm breaking some rules here with dynamic SQL but I still need to ask. I've inherited a table that contains a series of tags for each ticket that I need to pull records from.
Simple example... I have an array that contains "'Apples','Oranges','Grapes'" and I am trying to retrieve all records that contain ALL items contained within the array.
My SQL looks like this:
SELECT * FROM table WHERE basket IN ( " + fruitArray + " )
Which of course would be the equivalent of:
SELECT * FROM table WHERE basket = 'Apples' OR basket = 'Oranges' OR basket = 'Grapes'
I'm curious if there is a function that works the same as IN ( array ) except that it uses AND instead of OR so that I can obtain the same results as:
SELECT * FROM table WHERE basket LIKE '%Apples%' AND basket LIKE '%Oranges%' AND basket LIKE '%Grapes%'
I could probably just generate the entire string manually, but would like a more elegant solution if at all possible. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您需要进行
LIKE
比较,我认为您不走运。如果您要对涉及任意顺序的匹配集进行精确比较,则应查看SELECT
语句的INTERSECT
和EXCEPT
选项。它们有点令人困惑,但功能却相当强大。 (您必须将分隔字符串解析为表格格式,但当然您无论如何都会这样做,不是吗?)If you need to do
LIKE
comparisons, I think you're out of luck. If you are doing exact comparisons invovling matching sets in arbitrary order, you should look into theINTERSECT
andEXCEPT
options of theSELECT
statement. They're a bit confusing, but can be quite powerful. (You'd have to parse your delimited strings into tabular format, but of course you're doing that anyway, aren't you?)您要搜索的商品在购物篮中的顺序是否始终相同?如果是,单个 LIKE 就足够了:
并且用 % 分隔符将数组连接成字符串应该很简单。
Are the items you're searching for always in the same order within the basket? If yes, a single LIKE should suffice:
And concatenating your array into a string with % separators should be trivial.
这是 SQL 中非常常见的问题。基本上有两种解决方案:
匹配列表中的所有行,按所有这些行上具有公共值的列进行分组,并确保组中不同值的计数是数组中元素的数量。
对数组中的每个不同值进行自连接;只有这样您才能在一个 WHERE 表达式中比较多行的值。
This is a very common problem in SQL. There are basically two solutions:
Match all rows in your list, group by a column that has a common value on all those rows, and make sure the count of distinct values in the group is the number of elements in your array.
Do a self-join for each distinct value in your array; only then you can compare values from multiple rows in one WHERE expression.
在全文搜索中可能存在类似的情况,但总的来说,我真诚地怀疑这样的运算符除了与
LIKE
结合之外是否会非常有用。考虑一下:
它总是匹配零行。
There may be something like that in full text search, but in general, I sincerely doubt such an operator would be very useful, outside the conjunction with
LIKE
.Consider:
it would always match zero rows.
如果
basket
是一个字符串,就像您的示例所示,那么您可以获得的最接近的结果是使用LIKE '%apples%oranges%grapes%'
,它可以构建可以轻松使用'%'.implode('%', $tags).'%'
问题是某些“标签”是否可能包含在其他标签中,例如
'supercalifragilisticexpialidocious' LIKE '%super%'
将为 true。If
basket
is a string, like your example suggests, then the closest you could get would be to useLIKE '%apples%oranges%grapes%'
, which could be built easily with'%'.implode('%', $tags).'%'
The issue with this is if some of 'tags' might be contained in other tags, e.g.
'supercalifragilisticexpialidocious' LIKE '%super%'
will be true.