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

时间:2024-11-01 17:13:50

示例

--建表并插入数
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/distributed-devg-v8-gaussdb/gaussdb-12-0683.html