【SQL】SQL语句多表联合查询
SQL語句多表聯合查詢
CREATE TABLE orders(
id int not null primary key identity(1,1),
customerName varchar(100),
orderDate varchar(100),
orderPrice float
);
insert into orders(customerName,orderDate,orderPrice)values(‘B’,‘2018-3-1’,1000);
insert into orders(customerName,orderDate,orderPrice)values(‘C’,‘2018-2-30’,1600);
insert into orders(customerName,orderDate,orderPrice)values(‘B’,‘2018-3-6’,700);
insert into orders(customerName,orderDate,orderPrice)values(‘B’,‘2018-1-1’,1000);
insert into orders(customerName,orderDate,orderPrice)values(‘A’,‘2017-3-1’,2000);
insert into orders(customerName,orderDate,orderPrice)values(‘C’,‘2017-12-1’,100);
select *from orders;
–1.distinct 去除重復
SELECT distinct customerName FROM Orders
–2、GROUP BY 提取組合 并去重
SELECT customerName, orderPrice
FROM orders
GROUP BY customerName, orderPrice
—3、GROUP BY + COUNT 提取組合 計算重復 ,
SELECT customerName, orderPrice, count() as 重復數
FROM orders
GROUP BY customerName, orderPrice ;
– Count(1)和Count()實際上的意思是,評估Count()中的表達式是否為NULL,如果為NULL則不計數,而非NULL則會計數。
SELECT customerName, orderPrice, count(*) as 重復數
FROM orders
GROUP BY customerName, orderPrice ;
– COUNT根據select后的列進行查詢,有則計數。不管其他的列。
SELECT CustomerName, COUNT(1) AS 重復數
FROM Orders
GROUP BY CustomerName --記錄Customer 每種值的記錄數
–5、sum 和 GROUP BY
SELECT CustomerName, sum(orderPrice) AS sumOrderPrice
FROM Orders
GROUP BY CustomerName --記錄Customer 每種值的對應的OrderPrice的累加和
–6、AVG 和 GROUP BY 的多表聯合查詢 不加where
SELECT B.id, A.CustomerName, AVG(OrderPrice) AS 平均值
FROM Orders AS A,t_Admin AS B
GROUP BY B.id, A.CustomerName ;
–7、AVG 和 GROUP BY 的多表聯合查詢 加上where
SELECT B.id, A.customerName, AVG(OrderPrice) AS 平均值
FROM orders AS A,t_Admin AS B
WHERE A.id = B.id
GROUP BY B.id, A.customerName;
SELECT * FROM orders;
select * from t_Admin;
??對數據分析、機器學習、數據科學、金融風控等感興趣的小伙伴,需要數據集、代碼、行業報告等各類學習資料,可關注微信公眾號:風控圏子(別打錯字,是圏子,不是圈子,算了直接復制吧!)
??關注公眾號后,可聯系圈子助手加入我們的機器學習風控討論群和反欺詐討論群。(記得要備注喔!)
??相互學習,共同成長。
總結
以上是生活随笔為你收集整理的【SQL】SQL语句多表联合查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【大数据-Hadoop】dbeaver
- 下一篇: 【DBA】DBA——数据库管理员