postgresql學習三

avg():返回平均值

select avg(age) as b from company;
          b          
---------------------
 25.6666666666666667
(1 row)

sum():返回指定字段的總和

mydb=# select sum(age) as b from company;
  b  
-----
 154
(1 row)

count():返回查詢的記錄總數

select count(age) as b from company;
 b 
---
 6
(1 row)

current_timestamp:查詢當前時間

select current_timestamp;
             now              
------------------------------
 2020-05-08 16:04:59.90667+08
(1 row)

and:並且

SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;

or:或者

SELECT * FROM COMPANY WHERE AGE >= 25 or SALARY >= 65000;
 id | name  | age |  address   | salary 
----+-------+-----+------------+--------
  1 | Paul  |  32 | California |  20000
  2 | Allen |  25 | Texas      |  15000
  4 | Mark  |  25 | Rich-Mond  |  65000
  5 | David |  27 | Texas      |  85000

not:非

SELECT * FROM COMPANY WHERE AGE IS NOT NULL;

like:像、匹配

select * from company where name like 'Te%';
 id | name  | age | address | salary 
----+-------+-----+---------+--------
  3 | Teddy |  23 | Norway  |  20000

in查找25或者27

select * from company where age in (25,27);
 id | name  | age |  address  | salary 
----+-------+-----+-----------+--------
  2 | Allen |  25 | Texas     |  15000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000

between and:在什麼之間

select * from company where age between 25 and 27;
 id | name  | age |  address  | salary 
----+-------+-----+-----------+--------
  2 | Allen |  25 | Texas     |  15000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
(3 rows)

子查詢:

select age from company where exists (select * from company where salary > 15000);
 age 
-----
  32
  25
  23
  25
  27
  22
(6 rows)

limit:限制查詢數量

select * from company limit 4;
 id | name  | age |  address   | salary 
----+-------+-----+------------+--------
  1 | Paul  |  32 | California |  20000
  4 | Mark  |  25 | Rich-Mond  |  65000
  5 | David |  27 | Texas      |  85000
  6 | Kim   |  22 | South-Hall |  45000
(4 rows)

offset:設置偏移量比如從第三條數據開始

SELECT * FROM COMPANY LIMIT 3 OFFSET 2;
 id | name  | age |  address   | salary 
----+-------+-----+------------+--------
  5 | David |  27 | Texas      |  85000
  6 | Kim   |  22 | South-Hall |  45000
  3 | Teddy |  23 | Norway     |  15000
(3 rows)

order by:排序,默認升序,desc表示降序

select * from company where salary > 15000 order by age;
 id | name  | age |  address   | salary 
----+-------+-----+------------+--------
  6 | Kim   |  22 | South-Hall |  45000
  4 | Mark  |  25 | Rich-Mond  |  65000
  5 | David |  27 | Texas      |  85000
  1 | Paul  |  32 | California |  20000

group by:分組

SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME;
select name, avg(salary) from company group by name order by name;
 name  |          avg           
-------+------------------------
 David |     85000.000000000000
 James |  5000.0000000000000000
 Kim   |     45000.000000000000
 Mark  |     65000.000000000000
 Paul  |     20000.000000000000
 Teddy | 15000.0000000000000000
(6 rows)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章