为什么在sqlite中,`select s select(选择1联合选择2联合选择3)相交3联合3联合选择4“等于4,而不是3?

发布于 2025-02-05 03:13:30 字数 466 浏览 3 评论 0原文

在sqlite中,如果我输入:

SELECT (SELECT 1 UNION SELECT 2 UNION SELECT 3) INTERSECT SELECT 3 UNION SELECT 4

我得到结果4。怎么可能?

选择1联合选择2选择3(1,2,3),对吗? 选择3联合选择4(3,4)。因此,相交应该为3,对吗?我出了什么问题?

编辑:说相互评估首先评估我的问题,因为((1,2,3)Intersect(3))Union(4) is (3,4),而不是4

In SQLite, if I type:

SELECT (SELECT 1 UNION SELECT 2 UNION SELECT 3) INTERSECT SELECT 3 UNION SELECT 4

I get the result 4. How is that possible?

SELECT 1 UNION SELECT 2 SELECT 3 is (1, 2, 3), right? And SELECT 3 UNION SELECT 4 is (3, 4). So, the intersect should be 3, right? What am I getting wrong?

EDIT: Saying that INTERSECT is evaluated first does not answer my question, as ((1,2,3) INTERSECT (3)) UNION (4) is (3,4), rather than 4.

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

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

发布评论

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

评论(1

请别遗忘我 2025-02-12 03:13:30

如果您以这样的方式编写语句:

SELECT (SELECT 1 UNION SELECT 2 UNION SELECT 3) 
INTERSECT 
SELECT 3 
UNION 
SELECT 4

您可以看到您将3 选择语句与操作员UnionIntersect
所有3个语句都应返回相同数量的列。
您的第一个语句:

SELECT (SELECT 1 UNION SELECT 2 UNION SELECT 3)

实际上仅返回1行带1列,,,

因此,您的代码等同于:

SELECT 1 
INTERSECT 
SELECT 3 
UNION 
SELECT 4

Intersect,最后Union返回4

您打算写:

SELECT * FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3) 
INTERSECT 
SELECT 3 
UNION 
SELECT 4

如果 结果将是(3,4)

If you write your statement like this:

SELECT (SELECT 1 UNION SELECT 2 UNION SELECT 3) 
INTERSECT 
SELECT 3 
UNION 
SELECT 4

you can see that you are combining 3 SELECT statements with the operators UNION and INTERSECT.
All 3 statements should return the same number of columns.
Your 1st statement:

SELECT (SELECT 1 UNION SELECT 2 UNION SELECT 3)

actually returns only 1 row with 1 column, try it, which is the 1st row and the result is 1.

So your code is equivalent to:

SELECT 1 
INTERSECT 
SELECT 3 
UNION 
SELECT 4

which returns nothing for INTERSECT and finally UNION returns 4.

If you meant to write:

SELECT * FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3) 
INTERSECT 
SELECT 3 
UNION 
SELECT 4

then the result would be (3, 4).

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