Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 批量创建别名?_Postgresql - Fatal编程技术网

Postgresql 批量创建别名?

Postgresql 批量创建别名?,postgresql,Postgresql,我正在尝试连接三个表,产品,税收,类别。所有这些表都有冲突的列名。我知道我们通过创建别名来避免这一冲突 products: id name ... taxes id name ... categories id name ... 我的问题是,有没有方便的方法批量创建别名?我的意思是 SELECT products.* as product.*, taxes.* as tax.*, categories.* as cat

我正在尝试连接三个表,
产品
税收
类别
。所有这些表都有冲突的列名。我知道我们通过创建别名来避免这一冲突

products:
    id
    name
    ...

taxes
    id
    name
    ...

categories
    id
    name
    ...
我的问题是,有没有方便的方法批量创建别名?我的意思是

SELECT products.* as product.*, taxes.* as tax.*, categories.* as category.*
...
我希望结果集包含如下列:

product.id, product.name, tax.id, tax.name, ...
或者,我必须坚持做一些乏味的事情,比如:

SELECT products.id as product_id, products.name as product_name,
       taxes.id as tax_id, taxes.name as tax_name, ...

你在类固醇上搜索
*
。不幸的是,
SQL
中没有这样的功能

解决方案1:

在您喜爱的文本编辑器(vim、atom等)中使用块选择功能。将每列放入新行。块选择将
写入为
和表前缀。然后阻止选择并复制列名

解决方案2:

使用
信息\u架构生成选择列表。列

SELECT 
 string_agg(FORMAT('%s.%s AS %s_%s', "table_name",
                   column_name,"table_name", column_name), ', ')
FROM information_schema.columns
WHERE "table_name" IN ('products', 'taxes', 'categories');

您可以使用
E',\n'
将每列放在新行中

输出:

╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║                                                                                   string_agg                                                                                   ║
╠════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ products.id AS products_id, products.name AS products_name, taxes.id AS taxes_id, taxes.name AS taxes_name, categories.id AS categories_id, categories.name AS categories_name ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

你在类固醇上搜索
*
。不幸的是,
SQL
中没有这样的功能

解决方案1:

在您喜爱的文本编辑器(vim、atom等)中使用块选择功能。将每列放入新行。块选择将
写入为
和表前缀。然后阻止选择并复制列名

解决方案2:

使用
信息\u架构生成选择列表。列

SELECT 
 string_agg(FORMAT('%s.%s AS %s_%s', "table_name",
                   column_name,"table_name", column_name), ', ')
FROM information_schema.columns
WHERE "table_name" IN ('products', 'taxes', 'categories');

您可以使用
E',\n'
将每列放在新行中

输出:

╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║                                                                                   string_agg                                                                                   ║
╠════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ products.id AS products_id, products.name AS products_name, taxes.id AS taxes_id, taxes.name AS taxes_name, categories.id AS categories_id, categories.name AS categories_name ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝