赞
踩
初级的操作就是CRUD,但是高级的操作也是CRUD,只是语句写的更加复杂,不再是select * from table;这样简单,这次咱们学一些稍微高级点的。下面是上一篇文章。
在 PostgreSQL 中,union 是一种集合运算符,用于将两个或多个 SELECT 语句的结果合并成一个结果集。union 可以接受两个或多个 SELECT 语句作为参数,并将它们返回的结果合并成一个单独的结果集。
具体来说,union 运算符将两个 SELECT 语句的结果集合并,去除重复的行。合并的结果集将包含所有在至少一个 SELECT 语句中出现的行,但不包含在两个 SELECT 语句中都未出现的行。
- testdb=# select * from students;
- id | name | age | gender | class_id
- ----+--------+-----+--------+----------
- 1 | 张三 | 18 | 男 | 1
- 2 | 李四 | 19 | 女 | 1
- 3 | 王五 | 20 | 男 | 2
- 4 | 赵六 | 18 | 女 | 2
- 5 | 陈七 | 19 | 男 | 3
- 6 | 孙八 | 20 | 女 | 3
- 7 | 周九 | 18 | 男 | 4
- 8 | 吴十 | 19 | 女 | 4
- 9 | 郑十一 | 20 | 男 | 5
- 10 | 王十二 | 18 | 女 | 5
- (10 rows)
-
-
- testdb=# select * from students where age < 20
- union
- select * from students where class_id < 4;
- id | name | age | gender | class_id
- ----+--------+-----+--------+----------
- 5 | 陈七 | 19 | 男 | 3
- 10 | 王十二 | 18 | 女 | 5
- 2 | 李四 | 19 | 女 | 1
- 1 | 张三 | 18 | 男 | 1
- 4 | 赵六 | 18 | 女 | 2
- 6 | 孙八 | 20 | 女 | 3
- 7 | 周九 | 18 | 男 | 4
- 3 | 王五 | 20 | 男 | 2
- 8 | 吴十 | 19 | 女 | 4
- (9 rows)

第一个表获取到了年龄20下的学生,第二个表获取了前3个班的学生,然后通过union合并后,可以看到,结果的表既有20岁的学生,也有4班的学生,也就是将前两张表合并起来,但是去除了重复的行。
这边另外介绍一下Union all
- testdb=# select * from students where age < 20
- union all
- select * from students where class_id < 4;
- id | name | age | gender | class_id
- ----+--------+-----+--------+----------
- 1 | 张三 | 18 | 男 | 1
- 2 | 李四 | 19 | 女 | 1
- 4 | 赵六 | 18 | 女 | 2
- 5 | 陈七 | 19 | 男 | 3
- 7 | 周九 | 18 | 男 | 4
- 8 | 吴十 | 19 | 女 | 4
- 10 | 王十二 | 18 | 女 | 5
- 1 | 张三 | 18 | 男 | 1
- 2 | 李四 | 19 | 女 | 1
- 3 | 王五 | 20 | 男 | 2
- 4 | 赵六 | 18 | 女 | 2
- 5 | 陈七 | 19 | 男 | 3
- 6 | 孙八 | 20 | 女 | 3
- (13 rows)

Union 会自动去除多个结果集合中的重复行,只返回一个结果集。而 Union All 则会将所有结果集合中的行都返回,不管它们是否重复。因此,如果需要返回所有行,而不管它们是否重复,则
可以使用 Union All。
另外,Union 在合并结果时还会默认进行排序,而 Union All 则不会。如果需要对结果进行排序,可以使用 Order By 子句进行指定。
在使用 Union 或 Union All 时,需要确保两个结果集的列数和数据类型相等,否则可能会出现错误。而在使用 Union All 时,还可以使用类似 Select 语句中的通配符(如 * )来指定要合并的列。
NULL代表空值,插入数据的时候有些没插入的数据自然就是NULL,我们可以用IS NULL和NOT NULL来进行筛选。
我们建个表来看看:
- testdb=# CREATE TABLE engineer (
- id INT PRIMARY KEY,
- name VARCHAR(50),
- age INT,
- gender CHAR(1),
- address VARCHAR(200),
- created_at TIMESTAMP
- );
- CREATE TABLE
- testdb=# INSERT INTO engineer (id, name, age, gender, address, created_at) VALUES
- (1, 'John', 30, 'M', 'New York', '2023-02-18 10:00:00'),
- (2, 'Mary', 25, 'F', 'Los Angeles', '2023-02-18 10:00:00'),
- (3, 'Peter', 35, 'M', 'Chicago', '2023-02-18 10:00:00'),
- (4, 'Jane', 28, 'F', 'San Francisco', '2023-02-18 10:00:00'),
- (5, 'Bob', 40, 'M', 'Boston', '2023-02-18 10:00:00'),
- (6, ' NULL', 22, 'F', 'Washington DC', '2023-02-18 10:00:00'),
- (7, ' NULL', 38, 'M', 'Atlanta', '2023-02-18 10:00:00'),
- (8, ' NULL', 25, 'F', 'Miami', '2023-02-18 10:00:00'),
- (9, ' NULL', 32, 'M', 'Philadelphia', '2023-02-18 10:00:00'),
- (10, ' NULL', 28, 'F', 'Dallas', '2023-02-18 10:00:00');
- INSERT 0 10
-
- testdb=# select * from engineer;
- id | name | age | gender | address | created_at
- ----+-------+-----+--------+---------------+---------------------
- 1 | John | 30 | M | New York | 2023-02-18 10:00:00
- 2 | Mary | 25 | F | Los Angeles | 2023-02-18 10:00:00
- 3 | Peter | 35 | M | Chicago | 2023-02-18 10:00:00
- 4 | Jane | 28 | F | San Francisco | 2023-02-18 10:00:00
- 5 | Bob | 40 | M | Boston | 2023-02-18 10:00:00
- 6 | NULL | 22 | F | Washington DC | 2023-02-18 10:00:00
- 7 | NULL | 38 | M | Atlanta | 2023-02-18 10:00:00
- 8 | NULL | 25 | F | Miami | 2023-02-18 10:00:00
- 9 | NULL | 32 | M | Philadelphia | 2023-02-18 10:00:00
- 10 | NULL | 28 | F | Dallas | 2023-02-18 10:00:00
- (10 rows)

这边的NULL不是真的NULL值,只是名字叫做'NULL',我们修改下:
- testdb=# update engineer set name = null, age = null where name = ' NULL';
- UPDATE 5
- testdb=# select * from engineer;
- id | name | age | gender | address | created_at
- ----+-------+-----+--------+---------------+---------------------
- 1 | John | 30 | M | New York | 2023-02-18 10:00:00
- 2 | Mary | 25 | F | Los Angeles | 2023-02-18 10:00:00
- 3 | Peter | 35 | M | Chicago | 2023-02-18 10:00:00
- 4 | Jane | 28 | F | San Francisco | 2023-02-18 10:00:00
- 5 | Bob | 40 | M | Boston | 2023-02-18 10:00:00
- 6 | | | F | Washington DC | 2023-02-18 10:00:00
- 7 | | | M | Atlanta | 2023-02-18 10:00:00
- 8 | | | F | Miami | 2023-02-18 10:00:00
- 9 | | | M | Philadelphia | 2023-02-18 10:00:00
- 10 | | | F | Dallas | 2023-02-18 10:00:00
- (10 rows)
-
- testdb=# select * from engineer where name is null;
- id | name | age | gender | address | created_at
- ----+------+-----+--------+---------------+---------------------
- 6 | | | F | Washington DC | 2023-02-18 10:00:00
- 7 | | | M | Atlanta | 2023-02-18 10:00:00
- 8 | | | F | Miami | 2023-02-18 10:00:00
- 9 | | | M | Philadelphia | 2023-02-18 10:00:00
- 10 | | | F | Dallas | 2023-02-18 10:00:00
- (5 rows)
-
- testdb=# select * from engineer where age is not null;
- id | name | age | gender | address | created_at
- ----+-------+-----+--------+---------------+---------------------
- 1 | John | 30 | M | New York | 2023-02-18 10:00:00
- 2 | Mary | 25 | F | Los Angeles | 2023-02-18 10:00:00
- 3 | Peter | 35 | M | Chicago | 2023-02-18 10:00:00
- 4 | Jane | 28 | F | San Francisco | 2023-02-18 10:00:00
- 5 | Bob | 40 | M | Boston | 2023-02-18 10:00:00
- (5 rows)

这个很好说明,就是比如有些表的名字很长,所以我们就直接给个简称,依据上面的表我们给个例子。
- testdb=# select * from engineer as e where age is null;
- id | name | age | gender | address | created_at
- ----+------+-----+--------+---------------+---------------------
- 6 | | | F | Washington DC | 2023-02-18 10:00:00
- 7 | | | M | Atlanta | 2023-02-18 10:00:00
- 8 | | | F | Miami | 2023-02-18 10:00:00
- 9 | | | M | Philadelphia | 2023-02-18 10:00:00
- 10 | | | F | Dallas | 2023-02-18 10:00:00
- (5 rows)
-
-
- testdb=# select c.id as cid, c.name as cname, e.name as ename from company as c, engineer as e where c.id = e.id;
- cid | cname | ename
- -----+-------+-------
- 2 | CC | Mary
- 4 | Eoe | Jane
- 3 | Dod | Peter
- 5 | Fof | Bob
- 6 | Gog |
- (5 rows)

SQL 触发器是一种特殊的存储过程,它与普通的存储过程不同,它的执行是由事件来触发的,而不是由程序调用或手工启动的。当对 SQL 表进行操作(如 insert、delete、update 等)时,触发器会自动执行。触发器可以用于加强数据的完整性约束和业务规则等。
触发器可以分为两种类型:语句级触发器和行级触发器。语句级触发器在触发事件时执行一次,无论是否有行发生变化,而行级触发器只会在发生变化的行上执行。触发器可以设置触发时间,包括 before(在事件发生之前)和 after(在事件发生之后)两种。
触发器一般和函数一起使用。我们直接看个例子
- #创建一个函数
-
- CREATE OR REPLACE FUNCTION update_all_students_view()
- RETURNS TRIGGER
- AS $$
- BEGIN
- IF NEW.age > 0 THEN
- INSERT INTO all_students (name, age)
- VALUES (NEW.name, NEW.age);
- END IF;
- RETURN NEW;
- END;
- $$ LANGUAGE plpgsql;
-
- #创建一个触发器
-
- CREATE TRIGGER update_all_students
- AFTER INSERT
- ON students
- FOR EACH ROW
- WHEN (NEW.age > 0)
- EXECUTE FUNCTION update_all_students_view();
-
- #触发它
- testdb=# INSERT INTO students (name, age) VALUES ('张三', 18);
- INSERT INTO students (name, age) VALUES ('李四', 0);
-
- #这时候第二行就无法插入成功,因为年龄必须大于0
-
- #删除trigger
- drop trigger update_all_students on students;

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。