query 和 join 的查询结果不同
Different in results from query between query and join
你好,有这两个表:
#客户
cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email
'1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com'
'1000000002', 'Kids Place', '333 South Lake Drive', 'Columbus', 'OH', '43333', 'USA', 'Michelle Green', NULL
'1000000003', 'Fun4All', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'jjones@fun4all.com'
'1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', 'dstephens@fun4all.com'
'1000000005', 'The Toy Store', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard', NULL
#订单
order_num, order_date, cust_id
'20005', '2012-05-01 00:00:00', '1000000001'
'20006', '2012-01-12 00:00:00', '1000000003'
'20007', '2012-01-30 00:00:00', '1000000004'
'20008', '2012-02-03 00:00:00', '1000000005'
'20009', '2012-02-08 00:00:00', '1000000001'
现在,如果我在调用它的字段上使用子查询执行此查询,则为:
SELECT cust_name,
cust_state,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.cust_id = Customers.cust_id) AS orders
FROM Customers
ORDER BY cust_name;
我得到 'Kids Place' 订单 0:
cust_name, cust_state, orders
'Fun4All', 'IN', '1'
'Fun4All', 'AZ', '1'
'Kids Place', 'OH', '0'
'The Toy Store', 'IL', '1'
'Village Toys', 'MI', '2'
但是如果我使用加入版本:
SELECT c.cust_name,
c.cust_state,
COUNT(*) orders
FROM Customers c
INNER JOIN Orders o
ON o.cust_id = c.cust_id
GROUP BY o.cust_id
ORDER BY c.cust_name;
我没有得到 'Kids place' 行:
cust_name, cust_state, orders
'Fun4All', 'IN', '1'
'Fun4All', 'AZ', '1'
'The Toy Store', 'IL', '1'
'Village Toys', 'MI', '2'
为什么我会出现这种行为?
您需要 LEFT JOIN
才能使两者等价:
SELECT c.cust_name, c.cust_state,
COUNT(o.cust_id) as orders
FROM Customers c LEFT JOIN
Orders o
ON o.cust_id = c.cust_id
GROUP BY c.cust_id
ORDER BY c.cust_name;
另请注意,COUNT()
更改为计算匹配项数。
“INNER JOIN”只会 return 在连接两侧找到的结果(想象维恩图中的重叠区域)。因为没有 Kids Place 的订单,所以在连接中没有重叠,因此没有结果 returned.
如果您想使用联接对您的子查询进行类似的查询,那么您需要使用 LEFT JOIN。左连接将显示左侧的所有结果(在您的情况下为客户),然后尝试从右侧连接相应的结果(在您的情况下为订单)。
SELECT cust_name,
cust_state,
count(o.cust_id) as orders
FROM Customers c
LEFT JOIN Orders o on o.cust_id = c.cust_id
GROUP BY cust_name, cust_state;
示例 sqlfiddle
你好,有这两个表:
#客户
cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email
'1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com'
'1000000002', 'Kids Place', '333 South Lake Drive', 'Columbus', 'OH', '43333', 'USA', 'Michelle Green', NULL
'1000000003', 'Fun4All', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'jjones@fun4all.com'
'1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', 'dstephens@fun4all.com'
'1000000005', 'The Toy Store', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard', NULL
#订单
order_num, order_date, cust_id
'20005', '2012-05-01 00:00:00', '1000000001'
'20006', '2012-01-12 00:00:00', '1000000003'
'20007', '2012-01-30 00:00:00', '1000000004'
'20008', '2012-02-03 00:00:00', '1000000005'
'20009', '2012-02-08 00:00:00', '1000000001'
现在,如果我在调用它的字段上使用子查询执行此查询,则为:
SELECT cust_name,
cust_state,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.cust_id = Customers.cust_id) AS orders
FROM Customers
ORDER BY cust_name;
我得到 'Kids Place' 订单 0:
cust_name, cust_state, orders
'Fun4All', 'IN', '1'
'Fun4All', 'AZ', '1'
'Kids Place', 'OH', '0'
'The Toy Store', 'IL', '1'
'Village Toys', 'MI', '2'
但是如果我使用加入版本:
SELECT c.cust_name,
c.cust_state,
COUNT(*) orders
FROM Customers c
INNER JOIN Orders o
ON o.cust_id = c.cust_id
GROUP BY o.cust_id
ORDER BY c.cust_name;
我没有得到 'Kids place' 行:
cust_name, cust_state, orders
'Fun4All', 'IN', '1'
'Fun4All', 'AZ', '1'
'The Toy Store', 'IL', '1'
'Village Toys', 'MI', '2'
为什么我会出现这种行为?
您需要 LEFT JOIN
才能使两者等价:
SELECT c.cust_name, c.cust_state,
COUNT(o.cust_id) as orders
FROM Customers c LEFT JOIN
Orders o
ON o.cust_id = c.cust_id
GROUP BY c.cust_id
ORDER BY c.cust_name;
另请注意,COUNT()
更改为计算匹配项数。
“INNER JOIN”只会 return 在连接两侧找到的结果(想象维恩图中的重叠区域)。因为没有 Kids Place 的订单,所以在连接中没有重叠,因此没有结果 returned.
如果您想使用联接对您的子查询进行类似的查询,那么您需要使用 LEFT JOIN。左连接将显示左侧的所有结果(在您的情况下为客户),然后尝试从右侧连接相应的结果(在您的情况下为订单)。
SELECT cust_name,
cust_state,
count(o.cust_id) as orders
FROM Customers c
LEFT JOIN Orders o on o.cust_id = c.cust_id
GROUP BY cust_name, cust_state;
示例 sqlfiddle