目录
正文
一、初始数据
IF EXISTS(SELECT * FROM sysobjects s WHERE s.[name]='Customer' AND s.[type]='u')
DROP TABLE Customer
CREATE TABLE Customer(customerID NVARCHAR(50),city NVARCHAR(50))
GO
INSERT INTO Customer(customerID,city) VALUES('张三','北京')
INSERT INTO Customer(customerID,city) VALUES('李四','北京')
INSERT INTO Customer(customerID,city) VALUES('王五','北京')
INSERT INTO Customer(customerID,city) VALUES('赵六','西安')
IF EXISTS(SELECT * FROM sysobjects s WHERE s.[name]='Order' AND s.[type]='u')
DROP TABLE [Order]
CREATE TABLE [Order](orderID INT,customerID NVARCHAR(50))
GO
INSERT INTO [Order](orderID,customerID) VALUES( 1,'张三')
INSERT INTO [Order](orderID,customerID) VALUES( 2,'张三')
INSERT INTO [Order](orderID,customerID) VALUES( 3,'李四')
INSERT INTO [Order](orderID,customerID) VALUES( 4,'李四')
INSERT INTO [Order](orderID,customerID) VALUES( 5,'李四')
INSERT INTO [Order](orderID,customerID) VALUES( 6,'赵六')
INSERT INTO [Order](orderID,customerID) VALUES( 7, NULL)
customerID | City |
张三 | 北京 |
李四 | 北京 |
王五 | 北京 |
赵六 | 西安 |
orderID | customerID |
1 | 张三 |
2 | 张三 |
3 | 李四 |
4 | 李四 |
5 | 李四 |
6 | 赵六 |
7 | NULL |
二、查询语句
SELECT c.customerID,COUNT(o.orderID) cnt
FROM Customer c
LEFT OUTER JOIN [ORDER] o
ON c.customerID=o.customerID
WHERE c.city='北京'
GROUP BY c.customerID
HAVING COUNT(o.orderID)<3
ORDER BY c.customerID
三、查询结果
customerID | Cnt |
王五 | 0 |
张三 | 2 |
四、步骤
1、FROM JOIN
2、ON
3、LEFT OUTER
4、WHERE
5、GROUP BY
6、CUBE、ROLLUP
7、HAVING
8、SELECT
9、DISTINCT
10、ORDER BY
11、TOP
五、按步骤分析
1、FROM JOIN
4*7=28行记录。
c.customerID | c.city | o.orderID | o.customerID |
张三 | 北京 | 1 | 张三 |
张三 | 北京 | 2 | 张三 |
张三 | 北京 | 3 | 李四 |
张三 | 北京 | 4 | 李四 |
张三 | 北京 | 5 | 李四 |
张三 | 北京 | 6 | 赵六 |
张三 | 北京 | 7 | NULL |
李四 | 北京 | 1 | 张三 |
李四 | 北京 | 2 | 张三 |
李四 | 北京 | 3 | 李四 |
李四 | 北京 | 4 | 李四 |
李四 | 北京 | 5 | 李四 |
李四 | 北京 | 6 | 赵六 |
李四 | 北京 | 7 | NULL |
王五 | 北京 | 1 | 张三 |
王五 | 北京 | 2 | 张三 |
王五 | 北京 | 3 | 李四 |
王五 | 北京 | 4 | 李四 |
王五 | 北京 | 5 | 李四 |
王五 | 北京 | 6 | 赵六 |
王五 | 北京 | 7 | NULL |
赵六 | 西安 | 1 | 张三 |
赵六 | 西安 | 2 | 张三 |
赵六 | 西安 | 3 | 李四 |
赵六 | 西安 | 4 | 李四 |
赵六 | 西安 | 5 | 李四 |
赵六 | 西安 | 6 | 赵六 |
赵六 | 西安 | 7 | NULL |
2、ON
6行记录。
c.customerID | c.city | o.orderID | o.customerID |
张三 | 北京 | 1 | 张三 |
张三 | 北京 | 2 | 张三 |
李四 | 北京 | 3 | 李四 |
李四 | 北京 | 4 | 李四 |
李四 | 北京 | 5 | 李四 |
赵六 | 西安 | 6 | 赵六 |
3、LEFT OUTER
7行记录。
c.customerID | c.city | o.orderID | o.customerID |
张三 | 北京 | 1 | 张三 |
张三 | 北京 | 2 | 张三 |
李四 | 北京 | 3 | 李四 |
李四 | 北京 | 4 | 李四 |
李四 | 北京 | 5 | 李四 |
王五 | 北京 | NULL | NULL |
赵六 | 西安 | 6 | 赵六 |
4、WHERE
6行记录。
c.customerID | c.city | o.orderID | o.customerID |
张三 | 北京 | 1 | 张三 |
张三 | 北京 | 2 | 张三 |
李四 | 北京 | 3 | 李四 |
李四 | 北京 | 4 | 李四 |
李四 | 北京 | 5 | 李四 |
王五 | 北京 | NULL | NULL |
5、GROUP BY
分组 | 行 |
|
|
|
c.customerID | c.customerID | c.city | o.orderID | o.customerID |
张三 | 张三 | 北京 | 1 | 张三 |
| 张三 | 北京 | 2 | 张三 |
李四 | 李四 | 北京 | 3 | 李四 |
| 李四 | 北京 | 4 | 李四 |
| 李四 | 北京 | 5 | 李四 |
王五 | 王五 | 北京 | NULL | NULL |
6、CUBE、ROLLUP
略。
7、HAVING
分组 | 行 |
|
|
|
c.customerID | c.customerID | c.city | o.orderID | o.customerID |
张三 | 张三 | 北京 | 1 | 张三 |
| 张三 | 北京 | 2 | 张三 |
王五 | 王五 | 北京 | NULL | NULL |
8、SELECT
选取需要的字段。
9、DISTINCT
若有重复,去除重复。
10、ORDER BY
排序。
11、TOP
按TOP取记录数。