[英]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中的布局是这样的(请参阅列表号作为行号):
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就能做到?
It's probably going to be a lot simpler with 2 queries in PHP: 在PHP中使用2个查询可能会变得更加简单:
A first one to get the data from mapping
and display the first two rows (one loop over the results for each) 第一个从mapping
获取数据并显示前两行(一个循环遍历每个结果)
Another one to fetch the rest of the data and display them as individual rows 另一个用于获取其余数据并将其显示为单独的行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.