PostgreSQL语法、连接
博客地址:https://ask.hellobi.com/blog/zhiji 欢迎大家来交流学习。
本文总结与:PostgreSQL教程
1.语法
1.插入数据(INSERT语句)
在PostgreSQL中,INSERT
查询用于在表中插入新行。 可以一次插入单行或多行到表中。
- 语法:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
INSERT INTO EMPLOYEES( ID, NAME, AGE, ADDRESS, SALARY)
VALUES
(1, 'xsl', 23, '上海市浦东', 11000.00 ),
(2, 'xlm', 22, '上海市闵行', 8000.00 ),
注意:column1, column2, column3,...columnN是要插入数据的表中的列的名称。
2.查询数据(SELECT语句)
在PostgreSQL中,SELECT语句用于从数据库表中检索数据。 数据以结果表格的形式返回。 这些结果表称为结果集。
- 语法:
SELECT "column1", "column2".."column" FROM "table_name";
SELECT id,name FROM EMPLOYEES;
这里,column1,column2,.. columnN指定检索哪些数据的列。 如果要从表中检索所有字段,则必须使用以下语法:
SELECT * FROM "table_name";
3.更新数据(UPDATE语句)
在PostgreSQL中,UPDATE语句用于修改表中现有的记录。 要更新所选行,您必须使用WHERE子句,否则将更新所有行。
- 语法:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
UPDATE employ
SET age = 23,salary=11000
WHERE id=1;
4.删除数据(DELETE语句)
DELETE语句用于从表中删除现有记录。 “WHERE”子句用于指定删除所选记录的条件,如是不指定条件则将删除所有记录。
- 语法:
DELETE FROM table_name
WHERE [condition];
DELETE FROM EMPLOYEES
WHERE ID = 1;
5.数据排序(ORDER BY子句)
PostgreSQL ORDER BY子句用于按升序或降序对数据进行排序。数据在一列或多列的基础上进行排序。
- 语法:
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
参数说明:
- column_list:它指定要检索的列或计算。
- table_name:它指定要从中检索记录的表。FROM子句中必须至少有一个表。
- WHERE conditions:可选。 它规定必须满足条件才能检索记录。
- ASC:也是可选的。它通过表达式按升序排序结果集(默认,如果没有修饰符是提供者)。
- DESC:也是可选的。 它通过表达式按顺序对结果集进行排序。
#升序排序 - ORDER BY [field] ASC
SELECT *
FROM EMPLOYEES
ORDER BY AGE ASC;
#降序排序 - ORDER BY [field] DESC
SELECT *
FROM EMPLOYEES
ORDER BY name DESC;
6.分组(GROUP BY子句)
PostgreSQL GROUP BY子句用于将具有相同数据的表中的这些行分组在一起。 它与SELECT语句一起使用。
GROUP BY子句通过多个记录收集数据,并将结果分组到一个或多个列。 它也用于减少输出中的冗余。
- 语法:
SELECT column-list
FROM table_name
WHERE [conditions ]
GROUP BY column1, column2....columnN
ORDER BY column1, column2....columnN
SELECT NAME, SUM(SALARY)
FROM EMPLOYEES
GROUP BY NAME;
注意:在GROUP BY多个列的情况下,您使用的任何列进行分组时,要确保这些列应在列表中可用。
7.Having子句
在PostgreSQL中,HAVING子句与GROUP BY子句组合使用,用于选择函数结果满足某些条件的特定行。
- 语法:
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
#显示名称(name)数量小于2的记录
SELECT NAME
FROM EMPLOYEES
GROUP BY NAME HAVING COUNT (NAME) < 2;
8.条件查询
PostgreSQL条件的列表:
- AND 条件
- OR 条件
- AND & OR 条件
- NOT 条件
- LIKE 条件
- IN 条件
- NOT IN 条件
- BETWEEN 条件
语法:
- and 条件
SELECT column1, column2, ..... columnN
FROM table_name
WHERE [search_condition]
AND [search_condition];
SELECT *
FROM EMPLOYEES
WHERE SALARY > 120000
AND ID <= 4;
- or 条件
SELECT column1, column2, ..... columnN
FROM table_name
WHERE [search_condition]
OR [search_condition];
SELECT *
FROM EMPLOYEES
WHERE NAME = 'Minsu'
OR ADDRESS = 'Noida';
- and & or 条件
SELECT column1, column2, ..... columnN
FROM table_name
WHERE [search_condition] AND [search_condition]
OR [search_condition];
SELECT *
FROM EMPLOYEES
WHERE (NAME = 'Minsu' AND ADDRESS = 'Delhi')
OR (ID>= 8);
- not 条件
SELECT column1, column2, ..... columnN
FROM table_name
WHERE [search_condition] NOT [condition];
SELECT *
FROM EMPLOYEES
WHERE address IS NOT NULL ;
SELECT *
FROM EMPLOYEES
WHERE age NOT IN(21,24) ;
- LIKE 条件
SELECT column1, column2, ..... columnN
FROM table_name
WHERE [search_condition] LIKE [condition];
#查询名字以Ma开头的数据记录
SELECT *
FROM EMPLOYEES
WHERE NAME LIKE 'Ma%';
#查询地址中含有大道的数据记录
SELECT *
FROM EMPLOYEES
WHERE address LIKE '%大道%';
- IN条件
SELECT column1, column2, ..... columnN
FROM table_name
WHERE [search_condition] IN [condition];
#查询employee表中那些年龄为19,21的员工信息
SELECT *
FROM EMPLOYEES
WHERE AGE IN (19, 21);
- BETWEEN条件
SELECT column1, column2, ..... columnN
FROM table_name
WHERE [search_condition] BETWEEN [condition];
#查询employees表中年龄在24~27之间(含24,27)的数据信息
SELECT *
FROM EMPLOYEES
WHERE AGE BETWEEN 24 AND 27;
2.连接
在PostgreSQL中,有以下类型的连接:
- 内连接(INNER JOIN)
- 左外连接(LEFT OUTER JOIN)
- 右外连接(RIGHT OUTER JOIN)
- 全连接(FULL OUTER JOIN)
- 跨连接(CROSS JOIN)
1.INNER JOIN
语法:
SELECT table1.columns, table2.columns
FROM table1
INNER JOIN table2
ON table1.common_filed = table2.common_field;
SELECT EMPLOYEES.ID, EMPLOYEES.NAME, DEPARTMENT.DEPT
FROM EMPLOYEES
INNER JOIN DEPARTMENT
ON EMPLOYEES.ID = DEPARTMENT.ID;
2.左外连接
左外连接返回从“ON”条件中指定的左侧表中的所有行,只返回满足条件的另一个表中的行。语法:
SELECT table1.columns, table2.columns
FROM table1
LEFT OUTER JOIN table2
ON table1.common_filed = table2.common_field;
SELECT EMPLOYEES.ID, EMPLOYEES.NAME, DEPARTMENT.DEPT
FROM EMPLOYEES
LEFT OUTER JOIN DEPARTMENT
ON EMPLOYEES.ID = DEPARTMENT.ID;
3.右外连接
右外连接返回从“ON”条件中指定的右侧表中的所有行,只返回满足条件的另一个表中的行。语法:
SELECT table1.columns, table2.columns
FROM table1
RIGHT OUTER JOIN table2
ON table1.common_filed = table2.common_field;
SELECT EMPLOYEES.ID, EMPLOYEES.NAME, DEPARTMENT.DEPT
FROM EMPLOYEES
RIGHT OUTER JOIN DEPARTMENT
ON EMPLOYEES.ID = DEPARTMENT.ID;
4.全外连接
语法:
SELECT table1.columns, table2.columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_filed = table2.common_field;
SELECT EMPLOYEES.ID, EMPLOYEES.NAME, DEPARTMENT.DEPT
FROM EMPLOYEES
FULL OUTER JOIN DEPARTMENT
ON EMPLOYEES.ID = DEPARTMENT.ID;
5.跨连接(CROSS JOIN)
PostgreSQL跨连接(CROSS JOIN)将第一个表的每一行与第二个表的每一行相匹配。 它也被称为笛卡儿积分。 如果table1具有“x”列,而table2具有“y”列,则所得到的表将具有(x + y)列。
语法:
SELECT coloums
FROM table1
CROSS JOIN table2
SELECT NAME, DEPT
FROM EMPLOYEES
CROSS JOIN DEPARTMENT;