倍数评估“IN” “WHERE”中的表达式 mysql 中的子句
根据 @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 | +----------+----------+----------+
假设返回的仅行是1
和3
(而不是<代码>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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此查询将返回行,其中
b
是5
或7
,并且c
是4< /代码>。
“成对评估”是什么意思?
更新:
我将在示例中再添加一行:
如果您想匹配整个集合,可以使用以下语法:
这意味着:“返回
b
为2
且c
同时为3
,或b
为7
> 和с
同时为9
。”在上面的示例中,此查询将返回行
1
和3
但如果您以相反的方式重写此查询,如下所示:
,这将意味着“返回其中的所有行”
b
是2
或7
,并且c
是3
或>9
)。这将返回行
1
、3
和5
,因为行5
满足第二个查询的条件,但不是第一个查询的条件。This query will return rows, where
b
is either5
or7
, ANDc
is4
.What do you mean by "evaluation in pairs?"
Update:
I'll add one more row to the sample:
If you want to match the whole sets, you can use this syntax:
This means: "return all rows where
b
is2
andc
is3
at the same time, ORb
is7
andс
is9
at the same time."In the example above, this query will return rows
1
and3
But if you rewrite this query the other way around, like this:
, this will mean "return all rows where
b
is either2
or7
, ANDc
is either3
or9
).This will return rows
1
,3
and5
, since row5
satisfies the condition for the second query but not for the first one.第 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)您可以按顺序评估每个条件,这可能会让您更好地了解这里发生的情况。 您的查询表明应选择 b 为 5 或 7 并且 c 为 4 的所有值,因此让我们使用第一个条件 (
b IN (5,7)
) 来缩减表:现在,让我们评估下一个条件,两者都必须为 true 才能选择行(
c IN (4,4)
,其本质上与c = 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)
):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 asc = 4
):Everything else is valid:
是的,我相信你是对的。
实际上,“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)
您的示例并不能准确说明您的问题,但是多个 IN 子句彼此不相关; 它们像任何其他 WHERE 子句一样按顺序求值。
因此,以下查询
将匹配以下任意内容:
IN
可以被视为 or 分隔比较的简写。 这意味着前面的查询也可以写成(机制略有不同,但概念是相同的):因此,在您的示例中,是的,返回的唯一行是 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
will match any of the following:
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):So, in your example, yes, the only rows returned are 2 and 4. But it is not quite for the reason you suppose.