SQLite 基础17

2021-12-01 17:32:36 浏览数 (1)

GROUP BY

代码语言:javascript复制
sqlite> select age,sum(salary) from company group by age order by age ;
age         sum(salary)
----------  -----------
22          45000.0    
23          20000.0    
24          30000.0    
25          80000.0    
27          85000.0    
32          20000.0    
44          5000.0     
45          5000.0     
sqlite> select name,sum(salary) from company group by name order by name desc;
name        sum(salary)
----------  -----------
Teddy       20000.0    
Paul        40000.0    
Mark        65000.0    
Kim         45000.0    
James       20000.0    
David       85000.0    
Allen       15000.0    
sqlite>

HAVING

在最终结果中进行过滤

代码语言:javascript复制
sqlite> select name,count(*) from company group by name;
name        count(*)  
----------  ----------
Allen       1         
David       1         
James       3         
Kim         1         
Mark        1         
Paul        2         
Teddy       1         
sqlite> select name,count(*) from company group by name having count(*) < 2;
name        count(*)  
----------  ----------
Allen       1         
David       1         
Kim         1         
Mark        1         
Teddy       1         
sqlite> 

DISTINCT

去重

代码语言:javascript复制
sqlite> select name from company;
name      
----------
Paul      
Allen     
Teddy     
Mark      
David     
Kim       
James     
Paul      
James     
James     
sqlite> select distinct name from company;
name      
----------
Paul      
Allen     
Teddy     
Mark      
David     
Kim       
James     
sqlite>

0 人点赞