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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值