我不明白 select 的 SQL 行为
给定两个表:
A 行: [1641, 1468] 除其他外 B 行:[1641, 1468] 除其他外
如果我运行此:
select distinct A.c1, B.c2
from A, B
where A.c1 <> B.c1 and A.c2 <> B.c2
我预计不会取回该元组。尽管运行命令时它会返回该元组。
这种行为有解释吗?使用 SQLite。
Given two tables :
A with row : [1641, 1468] Amongst others
B with row : [1641, 1468] Amongst others
If I run this :
select distinct A.c1, B.c2
from A, B
where A.c1 <> B.c1 and A.c2 <> B.c2
I was expecting not to get that tuple back. Although when running the command it returns that tuple.
Is there an explanation for this behavior ? Using SQLite.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一方面,您的加入正在生成笛卡尔积。
这将导致:
因此,在运行查询时,where 子句将限制第一行和最后一行。这将使我们得到:
运行完整的查询将导致:
但是,我们可以构造一些行,这样我们就可以得到它,因为您使用的是
A.c1
和B .c2
:这将导致一行包含 [1641, 1468]。
For one thing, your join is producing a Cartesian product.
Which will result in:
So, when running your query, the where clause will restrict the first row and the last row. Which will leave us with:
And running your full query, will result in:
But, we can construct some rows so that we will get it regardless since you're using
A.c1
andB.c2
:Which will result in a row with [1641, 1468].
无法复制这一点。
评论后。 。 。
仍然没有得到元组{1641, 1468}。
如果您只想要不重复的行,您可以尝试更多类似的操作。
Can't replicate that.
After comments . . .
Still don't get the tuple {1641, 1468}.
If you just want the rows that aren't duplicated you might try something more along these lines.
如果你有
。
连接将首先产生
具有不同且仅选定的列,
我怀疑您想要类似的东西
If you have
.
The join will first produce
With distinct and just the selected columns it will be
I suspect you want something like
出于好奇,我在 ms sql server 05 上运行了以下测试:
我的结果是:
如您所见,我没有得到 Kassym 所期望的原始元组。
我猜 sqllite 是不同的,但我希望它也能按照 Kassym 预期的方式工作。
-m
Out of curiosity, I ran the following test on ms sql server 05:
my results were:
As you can see, I did not get that original tuple back which is what Kassym was expecting.
I guess sqllite is different, but I would have expected it to work the way Kassym expected as well.
-m