倍数评估“IN” “WHERE”中的表达式 mysql 中的子句

发布于 2024-07-29 13:41:47 字数 870 浏览 8 评论 0原文

根据 @Cesar 的请求进行更新。 希望我明白你想要什么,如果没有,请回复。 Quassnoi。

如果我执行如下 SQL 查询:SELECT * FROM TABLE_NAME WHERE b IN (2, 7) AND c IN (3, 9),我可以假设 MySQL 将仅匹配每个列表中具有相同编号的元素对吗?

即,(2, 3)(7, 9),...?

例如,假设我们有一个像这样的表:

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     1    |     2    |     3    |
 +----------+----------+----------+
 |     2    |     5    |     4    |
 +----------+----------+----------+
 |     3    |     7    |     9    |
 +----------+----------+----------+
 |     4    |     7    |     4    |
 +----------+----------+----------+
 |     5    |     2    |     9    |
 +----------+----------+----------+

假设返回的行是13(而不是<代码>5)?

Updating by @Cesar's request. Hope I understood what you want, if not, please revert. Quassnoi.

If I make an SQL query like this: SELECT * FROM TABLE_NAME WHERE b IN (2, 7) AND c IN (3, 9), can I assume that MySQL will match only pairs from elements with same number in each list?

That is, (2, 3), (7, 9), ...?

For example, suppose we have a table like this:

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     1    |     2    |     3    |
 +----------+----------+----------+
 |     2    |     5    |     4    |
 +----------+----------+----------+
 |     3    |     7    |     9    |
 +----------+----------+----------+
 |     4    |     7    |     4    |
 +----------+----------+----------+
 |     5    |     2    |     9    |
 +----------+----------+----------+

Is it correct to assume that the only rows returned are 1 and 3 (and not 5)?

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

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

发布评论

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

评论(5

说不完的你爱 2024-08-05 13:41:47
从 TABLE_NAME WHERE b IN(5,7) AND c IN(4,4) 中选择 * 
  

此查询将返回行,其中 b57,并且 c4< /代码>。

“成对评估”是什么意思?

更新:

我将在示例中再添加一行:

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     1    |     2    |     3    |
 +----------+----------+----------+
 |     2    |     5    |     4    |
 +----------+----------+----------+
 |     3    |     7    |     9    |
 +----------+----------+----------+
 |     4    |     7    |     4    |
 +----------+----------+----------+
 |     5    |     2    |     9    |
 +----------+----------+----------+

如果您想匹配整个集合,可以使用以下语法:

SELECT  *
FROM    table_name
WHERE   (b, c) IN ((2, 3), (7, 9))

这意味着:“返回 b2c 同时为 3,或 b7 > 和 с 同时为 9。”

在上面的示例中,此查询将返回行 13

但如果您以相反的方式重写此查询,如下所示:

SELECT  *
FROM    table_name
WHERE   b IN (2, 7)
        AND c IN (3, 9)

,这将意味着“返回其中的所有行” b27,并且 c3>9)。

这将返回行 135,因为行 5 满足第二个查询的条件,但不是第一个查询的条件。

SELECT * FROM TABLE_NAME WHERE b IN(5,7) AND c IN(4,4)

This query will return rows, where b is either 5 or 7, AND c is 4.

What do you mean by "evaluation in pairs?"

Update:

I'll add one more row to the sample:

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     1    |     2    |     3    |
 +----------+----------+----------+
 |     2    |     5    |     4    |
 +----------+----------+----------+
 |     3    |     7    |     9    |
 +----------+----------+----------+
 |     4    |     7    |     4    |
 +----------+----------+----------+
 |     5    |     2    |     9    |
 +----------+----------+----------+

If you want to match the whole sets, you can use this syntax:

SELECT  *
FROM    table_name
WHERE   (b, c) IN ((2, 3), (7, 9))

This means: "return all rows where b is 2 and c is 3 at the same time, OR b is 7 and с is 9 at the same time."

In the example above, this query will return rows 1 and 3

But if you rewrite this query the other way around, like this:

SELECT  *
FROM    table_name
WHERE   b IN (2, 7)
        AND c IN (3, 9)

, this will mean "return all rows where b is either 2 or 7, AND c is either 3 or 9).

This will return rows 1, 3 and 5, since row 5 satisfies the condition for the second query but not for the first one.

忘年祭陌 2024-08-05 13:41:47

第 2 行和第 2 行的返回 4 是正确的,尽管您选择 (4,4) 可能会使其更加混乱,因为它是多余的。 AND 意味着该行必须满足两个条件才为真。 如果查询有 WHERE b IN(5,7) AND c IN(4,9),您将返回第 2、3 和 4 行。

如果你成对地思考,你需要拥有所有的组合。 例如,b IN(5,7) AND c IN(4,9) 将产生 (5,4)、(5,9)、(7,4) 和 (7,9)可行的组合,而不仅仅是 (5,4) 和 (7,9)

The return of rows 2 & 4 is correct, though your choice of (4,4) can make it a little more confusing, as it is redundant. The AND means that the row must satisfy both your conditions to be true. If the query had WHERE b IN(5,7) AND c IN(4,9), you would get rows 2, 3, and 4 returned.

If you think of it in pairs, you need to have all the combinations. e.g., b IN(5,7) AND c IN(4,9) would yield (5,4), (5,9), (7,4), and (7,9) as possible combinations that would work, and NOT just (5,4) and (7,9)

聊慰 2024-08-05 13:41:47

您可以按顺序评估每个条件,这可能会让您更好地了解这里发生的情况。 您的查询表明应选择 b 为 5 或 7 并且 c 为 4 的所有值,因此让我们使用第一个条件 (b IN (5,7)) 来缩减表:

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     1    |     2    |     3    | < No match
 +----------+----------+----------+
 |     2    |     5    |     4    | < Match
 +----------+----------+----------+
 |     3    |     7    |     9    | < Match
 +----------+----------+----------+
 |     4    |     7    |     4    | < Match
 +----------+----------+----------+

现在,让我们评估下一个条件,两者都必须为 true 才能选择行(c IN (4,4),其本质上与 c = 4 相同):

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     2    |     5    |     4    | < Match
 +----------+----------+----------+
 |     3    |     7    |     9    | < No match
 +----------+----------+----------+
 |     4    |     7    |     4    | < Match
 +----------+----------+----------+

其他一切都有效:

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     2    |     5    |     4    |
 +----------+----------+----------+
 |     4    |     7    |     4    |
 +----------+----------+----------+

You can evaluate each condition in order, it might give you a better idea on what is happening here. Your query states that all values should be selected where b is either 5 or 7 and c is 4, so let's reduce the table using first condition (b IN (5,7)):

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     1    |     2    |     3    | < No match
 +----------+----------+----------+
 |     2    |     5    |     4    | < Match
 +----------+----------+----------+
 |     3    |     7    |     9    | < Match
 +----------+----------+----------+
 |     4    |     7    |     4    | < Match
 +----------+----------+----------+

Now, let's evaluate the next condition, both must be true in order for a row to be selected (c IN (4,4), hich is essentially the same as c = 4):

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     2    |     5    |     4    | < Match
 +----------+----------+----------+
 |     3    |     7    |     9    | < No match
 +----------+----------+----------+
 |     4    |     7    |     4    | < Match
 +----------+----------+----------+

Everything else is valid:

 +----------+----------+----------+
 |    PK    |     b    |     c    |
 +----------+----------+----------+
 |     2    |     5    |     4    |
 +----------+----------+----------+
 |     4    |     7    |     4    |
 +----------+----------+----------+
煮酒 2024-08-05 13:41:47

是的,我相信你是对的。

实际上,“IN”可以被视为 (b = 5 OR b = 7) 的简写。 这不是它“幕后”的工作方式,但这是一种简单的思考方式。

对于大型表,多个“IN”子句会导致性能问题。

编辑:

上面的海报是正确的, c IN (4, 4) 毫无意义。 您可以轻松地说“AND c = 4”。

如果将这些子句转换为其逻辑等效项,您就会明白原因:

SELECT * FROM table
其中 (b = 5 或 b = 7) AND (c = 4 或 c = 4)

Yes, I believe you are correct.

Effectively, 'IN' can be considered shorthand for (b = 5 OR b = 7). This is NOT how it works 'under the hood', but it is an easy way to think of it.

For large tables, multiple 'IN' clauses will cause performance issues.

EDIT:

The above poster is correct, c IN (4, 4) is pointless. You could just as easily say 'AND c = 4'.

If you convert the clauses to their logical equivalents, you see why:

SELECT * FROM table
WHERE (b = 5 OR b = 7) AND (c = 4 OR c = 4)

十六岁半 2024-08-05 13:41:47

您的示例并不能准确说明您的问题,但是多个 IN 子句彼此不相关; 它们像任何其他 WHERE 子句一样按顺序求值。

因此,以下查询

SELECT * FROM FOO WHERE b IN(5,7) AND c IN(4,8)

将匹配以下任意内容:

b  c
----
5  4
5  8
7  4
7  8

IN 可以被视为 or 分隔比较的简写。 这意味着前面的查询也可以写成(机制略有不同,但概念是相同的):

SELECT * FROM FOO WHERE (b = 5 OR b = 7) AND (c = 4 OR c = 8)

因此,在您的示例中,是的,返回的唯一行是 2 和 4。但这并不完全是因为原因你认为。

Your example does not exactly illustrate your question, but multiple IN clauses are not related to one another; they are evaluated in sequence like any other WHERE clause.

Thus, the following query

SELECT * FROM FOO WHERE b IN(5,7) AND c IN(4,8)

will match any of the following:

b  c
----
5  4
5  8
7  4
7  8

IN can be considered shorthand for or-separated comparisons. This means the previous query can also be written as (the mechanics are slightly different, but the concept is the same):

SELECT * FROM FOO WHERE (b = 5 OR b = 7) AND (c = 4 OR c = 8)

So, in your example, yes, the only rows returned are 2 and 4. But it is not quite for the reason you suppose.

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