如何使用 PostgreSQL 中动态生成的列名来比较两个表的子集?
我有两张表,一张是测试表,一张是生产表,两者都有 200 多个列和几千行代码来创建该表。我定期进行更改并尝试自动化 QA。我想
- 比较两个表之间的所有行以检测差异。
- 排除某些列,因为列是新列(添加到测试中,产品中不存在),或者因为它们有意不同(table_creation、created_by_used_id 等)。
- 使用变量生成
SELECT list_of_column_names
,这样我就不必不断手动更新需要在两个表之间进行比较的列名称。
#3 是问题所在。我知道如何在 python 中执行此操作,但目前仅限于在 PostgreSQL 中执行此操作,并且从未对 SQL 中的变量执行过任何操作。
代码到目前为止到目前为止
,我知道我可以从那里获取所有列名称从
SELECT *
FROM information_schema.columns
WHERE table_schema = 'my_test_schema'
AND table_name = 'my_test_table'
那里,我可以执行 FULL JOIN 和 WHERE 子句来与 prod 列连接,并获取一个只有 1 列我想要的子集列名称的表。
之后,我使用 EXCEPT/UNION ALL 脚本来比较表。下面的问题与 * 相关 - 我需要某种变量或列表并使用它来选择列名称。
SELECT * FROM my_test_table
EXCEPT
SELECT * FROM my_prod_table
UNION ALL
SELECT * from my_prod_table
EXCEPT
SELECT * from my_test_table
我愿意接受其他建议。
I have two tables, one a test table and one a production table, both with +200 columns and a couple thousand lines of code to create the table. I periodically make changes and am trying to automate QA. I would like to
- Compare all rows between the two tables to detect differences.
- Exclude certain columns, either because columns are new (added to test, does not exist in prod) or because they will be different on purpose (table_creation, created_by_used_id, etc).
- Use a variable to generate the
SELECT list_of_column_names
so I do not have to continually manually update the column names I need to compare between the two tables.
#3 is the issue. I know how to do this in python, but am currently limited to doing this only in PostgreSQL and have never done anything with variables in SQL.
Code So Far
So far, I know I can get all columns names from
SELECT *
FROM information_schema.columns
WHERE table_schema = 'my_test_schema'
AND table_name = 'my_test_table'
From there, I can do a FULL JOIN and WHERE clause to join with the prod columns and get a table with 1 column of only the subset column names that I want.
After that, I'm using an EXCEPT/UNION ALL script to compare the tables. The issue below is with the * - I instead need to have some sort of variable or list and use that to select the column names.
SELECT * FROM my_test_table
EXCEPT
SELECT * FROM my_prod_table
UNION ALL
SELECT * from my_prod_table
EXCEPT
SELECT * from my_test_table
I am open to alternate suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将为您提供 prod 表中存在的列,而不是
测试表和/或相反:
This will give you the columns which are present in prod table and not in
test table and or the other way around: