云数据库 GAUSSDB-复合查询:示例

时间:2024-11-13 14:45:47

示例

--建表并插入数据。
gaussdb=# CREATE TABLE test1(c11 INT, c12 VARCHAR);
gaussdb=# INSERT INTO test1 VALUES (1,'a'),(2,'b'),(4,'d');

gaussdb=# CREATE TABLE test2(c21 INT, c22 VARCHAR); 
gaussdb=# INSERT INTO test2 VALUES (1,'a'),(3,'c');
  • UNION
    gaussdb=# SELECT * FROM test1 UNION SELECT * FROM test2;
     c11 | c12 
    -----+-----
       1 | a
       4 | d
       2 | b
       3 | c
    (4 rows)
  • UNION ALL
    gaussdb=# SELECT * FROM test1 UNION ALL SELECT * FROM test2;
     c11 | c12 
    -----+-----
       1 | a
       2 | b
       4 | d
       1 | a
       3 | c
    (5 rows)
  • INTERSECT
    gaussdb=# SELECT * FROM test1 INTERSECT SELECT * FROM test2;
     c11 | c12 
    -----+-----
       1 | a
    (1 row)
  • MINUS
    gaussdb=# SELECT * FROM test1 MINUS SELECT * FROM test2;
     c11 | c12 
    -----+-----
       4 | d
       2 | b
    (2 rows)
    -- 删除。
    gaussdb=# DROP TABLE test1,test2;
support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-1707.html