gpt4 book ai didi

SQL 减号运算符

转载 作者:行者123 更新时间:2023-12-04 22:38:30 26 4
gpt4 key购买 nike

select deptno  
from emp2
MINUS
select deptno
from dpt
order by deptno;

上面的查询返回 = No Data Found

鉴于,

select deptno,ename   
from emp2
MINUS
select deptno,dname
from dpt
order by deptno;

返回所有 deptno 和 ename 字段值。

您能解释一下为什么尽管在第二个查询中使用了 MINUS 运算符,我还是得到了 deptno 字段的所有值吗?

legend: 
emp is employee table and dpt is department table,
ename is employee name -belonging to emp,
dname is department name -belonging to dpt,
deptno is department no. -common to both

最佳答案

在第一个查询中表之间没有不同的deptno,在第二个查询中你有相同的deptno,但名称不同

这样想:

查询 1:

select deptno  
from emp2
MINUS
select deptno
from dpt
order by deptno;

示例:

[1,2,3,4] - [1,2,3,4] = empty

查询 2:

select deptno,ename   
from emp2
MINUS
select dept no,dname
from dpt
order by deptno;

例子:

[(1, 'a'),(2, 'b'),(3, 'c'),(4, 'd')] - 
[(1, 'z'),(2, 'x'),(3, 'u'),(4, 'w')] =
[(1, 'a'),(2, 'b'),(3, 'c'),(4, 'd')]

MINUS

MINUS operator, which returns only unique rows returned by the first query but not by the second

关于SQL 减号运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32807232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com