2016-04-20 116 views
1

我们使用的是使用Postgres 8的Redshift。
我需要比较(2)几乎相同的表格,但其他表格会有额外的列,我需要找出列差异。postgresql - 得到两个表格之间的列的差异列表

例子:

CREATE TABLE table1 (
    v_id character varying(255) NOT NULL, 
    v_created timestamp without time zone NOT NULL, 
    abc_102 boolean, 
    abc_103 boolean, 
    abc_104 boolean, 
    def_56 boolean DEFAULT false NOT NULL, 
    def_57 boolean DEFAULT false NOT NULL 
) 

CREATE TABLE table2 (
    v_id character varying(255) NOT NULL, 
    v_created timestamp without time zone NOT NULL, 
    abc_102 boolean, 
    abc_103 boolean, 
    abc_104 boolean, 
    abc_105 boolean, 
    def_56 boolean DEFAULT false NOT NULL, 
    def_57 boolean DEFAULT false NOT NULL, 
    def_58 boolean DEFAULT false NOT NULL, 
) 

我可以使用哪些查询,这将使我的列差异列表?

回答

1

您可以选择从table2所有列名其中做也出现在table1实现这一目标:

SELECT column_name 
FROM information_schema.columns 
WHERE table_schema = 'your_schema' AND table_name = 'table2' 
    AND column_name NOT IN 
    (
     SELECT column_name 
     FROM information_schema.columns 
     WHERE table_schema = 'your_schema' AND table_name = 'table1' 
    ) 
+0

这对我的作品。谢谢。 – noober

相关问题