这个符号 (<>) 对这个 select 语句做了什么?
What's this symbol (<>) doing to this select statement?
下面的代码从数据库中检索了 74,700 行。
select * from Orders O
inner join customers C
on O.CustomerID <> c.CustomerID
与 = 相同的代码检索 830 条记录。
select * from Orders O
inner join customers C
on O.CustomerID = c.CustomerID
这不等于符号对我的搜索查询有什么影响?外连接也有同样的区别。
谢谢。
<>
是 SQL 中的 "not-equals" 运算符。
查询正在获取 customerId
列不同的所有对或订单和客户。您真正想要的可能是没有有效客户 ID 的订单:
select o.*
from orders o left join
customers c
on o.CustomerID = c.CustomerID
where c.CustomerId is null;
(实际上,如果您设置了适当的外键关系,这似乎不太可能。)
或者更有可能没有订单的客户:
select c.*
from customers c left join
orders o
on o.CustomerID = c.CustomerID
where o.CustomerId is null;
<> 符号表示不等于 ie) O.CustomerID 不等于 c.CustomerID 或
你可以使用 != 这也意味着不等于 sql
不等于 <>
运算符 returns 当值不等于
时为真
代码 on O.CustomerID <> c.CustomerID
似乎将订单的每一行 table 与不等于它的客户的每一行 table 连接起来。这是 SQL fiddle.
中的示例
http://sqlfiddle.com/#!9/e05f92/2/0
如您所见,在顶部 select(使用 = 符号的行),只有 selects 订单 customerID 等于客户 customerID 的行
在底部 select(使用 <> 的地方)它连接每个客户行,每个可能的订单行不相等,这就是为什么 <> 查询会得到这么多结果.
ON 运算符
从逻辑上讲,每个 SQL 查询都按以下顺序执行:
FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY
You can read about this further from the official documentation from MSDN. SELECT (Transact-SQL)
这意味着 on
谓词与 table 之间的基数匹配相关,而 WHERE
子句过滤结果。
Cardinal 表示 1:N,或一方的比赛数。在您的示例中,ON A.CUSTOMER_ID = B.CUSTOMER_ID
将为源 table
中的每个匹配集生成一行 return
LEFT
and RIGHT
refer to which side is the source table. By default, the
left is considered the source table.
所以如果tableA有3行,其中ID = 3
,那么即使tableB只有一个ID为3,你也会return3行; Table A 中的每一行都单独处理。
一个好的连接只使用 return 唯一连接所需的列数,因此不需要的重复值不会被 return 编辑。即使您打算使用 CROSS JOIN
,您仍然需要确保为您的目的使用独特的匹配集。
在关系上,连接是什么意思?
这是真正的问题:table 代表什么,它们如何回答问题?
关系意味着价值、信息、问题或查询的回答。
当您知道批处理或过程的作用或它对脚本的用途时,识别愚蠢的查询会变得更容易。
结论
ON ID = ID
- 选择匹配的行。
ON ID <> ID
- returns 每个不匹配的行 对于源 table 中的每一行 。本质上是交叉连接减去实际连接行。
好的做法是使用 ON 来识别匹配的唯一行,并使用 WHERE 子句在源端过滤此结果 table。
下面的代码从数据库中检索了 74,700 行。
select * from Orders O
inner join customers C
on O.CustomerID <> c.CustomerID
与 = 相同的代码检索 830 条记录。
select * from Orders O
inner join customers C
on O.CustomerID = c.CustomerID
这不等于符号对我的搜索查询有什么影响?外连接也有同样的区别。
谢谢。
<>
是 SQL 中的 "not-equals" 运算符。
查询正在获取 customerId
列不同的所有对或订单和客户。您真正想要的可能是没有有效客户 ID 的订单:
select o.*
from orders o left join
customers c
on o.CustomerID = c.CustomerID
where c.CustomerId is null;
(实际上,如果您设置了适当的外键关系,这似乎不太可能。)
或者更有可能没有订单的客户:
select c.*
from customers c left join
orders o
on o.CustomerID = c.CustomerID
where o.CustomerId is null;
<> 符号表示不等于 ie) O.CustomerID 不等于 c.CustomerID 或 你可以使用 != 这也意味着不等于 sql
不等于 <>
运算符 returns 当值不等于
代码 on O.CustomerID <> c.CustomerID
似乎将订单的每一行 table 与不等于它的客户的每一行 table 连接起来。这是 SQL fiddle.
http://sqlfiddle.com/#!9/e05f92/2/0
如您所见,在顶部 select(使用 = 符号的行),只有 selects 订单 customerID 等于客户 customerID 的行
在底部 select(使用 <> 的地方)它连接每个客户行,每个可能的订单行不相等,这就是为什么 <> 查询会得到这么多结果.
ON 运算符
从逻辑上讲,每个 SQL 查询都按以下顺序执行:
FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY
You can read about this further from the official documentation from MSDN. SELECT (Transact-SQL)
这意味着 on
谓词与 table 之间的基数匹配相关,而 WHERE
子句过滤结果。
Cardinal 表示 1:N,或一方的比赛数。在您的示例中,ON A.CUSTOMER_ID = B.CUSTOMER_ID
将为源 table
LEFT
andRIGHT
refer to which side is the source table. By default, the left is considered the source table.
所以如果tableA有3行,其中ID = 3
,那么即使tableB只有一个ID为3,你也会return3行; Table A 中的每一行都单独处理。
一个好的连接只使用 return 唯一连接所需的列数,因此不需要的重复值不会被 return 编辑。即使您打算使用 CROSS JOIN
,您仍然需要确保为您的目的使用独特的匹配集。
在关系上,连接是什么意思?
这是真正的问题:table 代表什么,它们如何回答问题? 关系意味着价值、信息、问题或查询的回答。
当您知道批处理或过程的作用或它对脚本的用途时,识别愚蠢的查询会变得更容易。
结论
ON ID = ID
- 选择匹配的行。ON ID <> ID
- returns 每个不匹配的行 对于源 table 中的每一行 。本质上是交叉连接减去实际连接行。
好的做法是使用 ON 来识别匹配的唯一行,并使用 WHERE 子句在源端过滤此结果 table。