简体   繁体   English

PostgreSQL PHP-在Excel / CSV导出中使用第二个表的值作为第一个表的值的列标题和列描述

[英]PostgreSQL PHP - Use values of second table as column header and column description for values of first table in Excel/CSV export

some time ago a big Excel file was imported into a PostgreSQL table. 不久前,一个大的Excel文件被导入到PostgreSQL表中。 The layout in Excel was like this (see list numbers as row numbers): Excel中的布局是这样的(请参阅列表号作为行号):

  1. Some Column | 一些专栏| Some other Column | 其他专栏| Yet another one | 又一个| ... ...
  2. Descriptiontext foo | 说明文字foo | Descriptiontext2 | 说明文字2 | bar foo bar | bar foo bar | ... ...
  3. Value a | 值| Value b | 值b | Value c | 值c | ... ...
  4. Value aa | 值aa | Value bb | 值bb | Value cc | 值cc | ... ...

So there was the header row with the column names, then a row with a description text for each column and after that hundreds of rows with all the values. 因此,标题行带有列名,然后是每一行带有描述文本的行,其后是数百行带有所有值的行。

In PostgreSQL two tables where created. 在PostgreSQL中创建了两个表。

Table1: 表格1:

CREATE TABLE t1 (
    t1_id integer NOT NULL
    col1 inet,
    some_other_col character varying,
    another_one integer,
    ...
);

Table2: 表2:

CREATE TABLE mapping (
    mapping_id integer NOT NULL
    colname character varying,
    mapping character varying,
    description character varying,
);

INSERT INTO mapping VALUES (1, 'col1', 'Some Column', 'Descriptiontext foo');
INSERT INTO mapping VALUES (2, 'some_other_col', 'Some other Column', 'Descriptiontext2');
INSERT INTO mapping VALUES (3, 'another_one', 'Yet another one', 'bar foo bar');

My question now is, how to merge these tables again to export an Excel sheet with the same format as the old one? 我现在的问题是,如何再次合并这些表以导出与旧表相同格式的Excel工作表? And would I need PHP for that or is this possible using only SQL? 我是否需要PHP还是仅使用SQL就能做到?

Just create a FUNCTION to merge both table with your desire format 只需创建一个功能即可将两个表与所需格式合并

Then use COPY to create the excel file. 然后使用COPY创建excel文件。 No need to use php here 无需在这里使用php

It's probably going to be a lot simpler with 2 queries in PHP: 在PHP中使用2个查询可能会变得更加简单:

  1. A first one to get the data from mapping and display the first two rows (one loop over the results for each) 第一个从mapping获取数据并显示前两行(一个循环遍历每个结果)

  2. Another one to fetch the rest of the data and display them as individual rows 另一个用于获取其余数据并将其显示为单独的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM