“IN”的等价物使用 AND 而不是 OR 逻辑?

发布于 2024-12-15 01:11:19 字数 675 浏览 4 评论 0原文

我知道我在这里使用动态 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 技术交流群。

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

发布评论

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

评论(5

樱花坊 2024-12-22 01:11:20

如果您需要进行 LIKE 比较,我认为您不走运。如果您要对涉及任意顺序的匹配集进行精确比较,则应查看 SELECT 语句的 INTERSECTEXCEPT 选项。它们有点令人困惑,但功能却相当强大。 (您必须将分隔字符串解析为表格格式,但当然您无论如何都会这样做,不是吗?)

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 the INTERSECT and EXCEPT options of the SELECT 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?)

一场信仰旅途 2024-12-22 01:11:20

您要搜索的商品在购物篮中的顺序是否始终相同?如果是,单个 LIKE 就足够了:

SELECT * FROM table WHERE basket LIKE '%Apples%Oranges%Grapes%';

并且用 % 分隔符将数组连接成字符串应该很简单。

Are the items you're searching for always in the same order within the basket? If yes, a single LIKE should suffice:

SELECT * FROM table WHERE basket LIKE '%Apples%Oranges%Grapes%';

And concatenating your array into a string with % separators should be trivial.

伤痕我心 2024-12-22 01:11:19

这是 SQL 中非常常见的问题。基本上有两种解决方案:

  1. 匹配列表中的所有行,按所有这些行上具有公共值的列进行分组,并确保组中不同值的计数是数组中元素的数量。

    从篮子中选择basket_id
    篮子在哪里('苹果','橙子','葡萄')
    按篮子 ID 分组
    有计数(不同的篮子)= 3
    
  2. 对数组中的每个不同值进行自连接;只有这样您才能在一个 WHERE 表达式中比较多行的值。

    SELECT b1.basket_id
    从篮子 b1
    INNER JOIN 篮子 b2 USING (basket_id)
    INNER JOIN 篮子 b3 USING (basket_id)
    WHERE (b1.basket, b2.basket, b3.basket) = ('苹果','橙子','葡萄')
    

This is a very common problem in SQL. There are basically two solutions:

  1. 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.

    SELECT basket_id FROM baskets
    WHERE basket IN ('Apples','Oranges','Grapes')
    GROUP BY basket_id
    HAVING COUNT(DISTINCT basket) = 3
    
  2. Do a self-join for each distinct value in your array; only then you can compare values from multiple rows in one WHERE expression.

    SELECT b1.basket_id
    FROM baskets b1
    INNER JOIN baskets b2 USING (basket_id)
    INNER JOIN baskets b3 USING (basket_id)
    WHERE (b1.basket, b2.basket, b3.basket) = ('Apples','Oranges','Grapes')
    
野生奥特曼 2024-12-22 01:11:19

在全文搜索中可能存在类似的情况,但总的来说,我真诚地怀疑这样的运算符除了与 LIKE 结合之外是否会非常有用。

考虑一下:

SELECT * FROM table WHERE basket ='Apples' AND basket = 'Oranges'

它总是匹配零行。

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:

SELECT * FROM table WHERE basket ='Apples' AND basket = 'Oranges'

it would always match zero rows.

动次打次papapa 2024-12-22 01:11:19

如果 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 use LIKE '%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.

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