Mysql - 将行转换为列

发布于 2024-12-12 07:43:45 字数 2224 浏览 0 评论 0原文

问题:

如何将下表中的数据导出到 cvs excel 文件,以便dog_attributes 中的所有属性都拥有自己的列(主要使用 MySQL)?

注意

  • 我将一次导出一个 group_id 以及该 group_id 的所有dog_attributes。
  • 每组可能有数千只狗和几百个 group_elements。我 认为主要使用 MySQL 而不是 PHP 循环会更好 MySQL 具有内置导出功能,因此速度更快。
  • 我的服务器正在运行带有 PHP 和 Codeigniter 框架的 MySQL。

示例:

有四张表。

group_tbl - 组由管理员设置,用于包含常见元素。

+----+---------+
| id | var1    |
+----+---------+
| 1  | data    |
| 2  | data    |
+----+---------+

group_elements_tbl - 包含管理员设置的元素。这些是由用户填写的表单字段。

+----+----------+-----------+
| id | group_id | elmt_name |
+----+----------+-----------+
| 1  | 1        | height    |
| 2  | 1        | color     |
| 3  | 2        | breed     |
+----+----------+-----------+

dogs_tbl - 包含每个用户创建的狗。

+----+----------+---------+
| id | group_id | name    |
+----+----------+---------+
| 1  | 1        | Rover   |
| 2  | 1        | Buck    |
| 3  | 2        | Rex     |
+----+----------+---------+

dog_attributes_tbl - 包含管理员在 groups_elements_tbl 中设置的自定义元素的值。

+----+--------------------+------------+
| id | group_elements_id  | attr_value |
+----+--------------------+------------+
| 1  | 1                  | 54 inches  |
| 2  | 2                  | brown      |
| 3  | 1                  | 34 inches  |
| 3  | 2                  | white      |
| 4  | 3                  | husky      |
+----+---------+------------+

最终结果:

组 1 的最终 Excel 电子表格 (group_id = 1):

+--------+----------+-----------+--------+
| dog_id | dog_name | height    | color  |
+--------+----------+-----------+--------+
| 1      | Rover    | 54 inches | brown  |
| 2      | Buck     | 34 inches | white  |
+--------+----------+-----------+--------+

组 2 的最终 Excel 电子表格 (group_id = 2):

+--------+----------+-----------+
| dog_id | dog_name | breed     |
+--------+----------+-----------+
| 3      | Rex      | husky     |
+--------+----------+-----------+

Question:

How do I export data in the tables below to a cvs excel file so all attributes in dog_attributes have their own column using mostly MySQL?

NOTES:

  • I will be exporting one group_id and all dog_attributes for that group_id at a time.
  • There could be thousands of dogs and a few hundred group_elements per group. I
    figured using mostly MySQL instead of a PHP loop would be better for
    speed since MySQL has a built in export function.
  • My server is running MySQL with PHP and Codeigniter Framework.

Example:

There are four tables.

group_tbl - groups are setup by the admin and used to contain common elements.

+----+---------+
| id | var1    |
+----+---------+
| 1  | data    |
| 2  | data    |
+----+---------+

group_elements_tbl - contains elements setup by the admin. These are form fields filled in by the user.

+----+----------+-----------+
| id | group_id | elmt_name |
+----+----------+-----------+
| 1  | 1        | height    |
| 2  | 1        | color     |
| 3  | 2        | breed     |
+----+----------+-----------+

dogs_tbl - contains dogs that each user has created.

+----+----------+---------+
| id | group_id | name    |
+----+----------+---------+
| 1  | 1        | Rover   |
| 2  | 1        | Buck    |
| 3  | 2        | Rex     |
+----+----------+---------+

dog_attributes_tbl - contains the values of the custom elements setup in the groups_elements_tbl by the admin.

+----+--------------------+------------+
| id | group_elements_id  | attr_value |
+----+--------------------+------------+
| 1  | 1                  | 54 inches  |
| 2  | 2                  | brown      |
| 3  | 1                  | 34 inches  |
| 3  | 2                  | white      |
| 4  | 3                  | husky      |
+----+---------+------------+

Final Results:

Final Excel Spreadsheet for group 1 (group_id = 1):

+--------+----------+-----------+--------+
| dog_id | dog_name | height    | color  |
+--------+----------+-----------+--------+
| 1      | Rover    | 54 inches | brown  |
| 2      | Buck     | 34 inches | white  |
+--------+----------+-----------+--------+

Final Excel Spreadsheet for group 2 (group_id = 2):

+--------+----------+-----------+
| dog_id | dog_name | breed     |
+--------+----------+-----------+
| 3      | Rex      | husky     |
+--------+----------+-----------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒中人 2024-12-19 07:43:45

最快的方法是以编程方式处理这些属性 - 不使用 SQL

您应该获取所有狗,在第二个请求中您感兴趣的狗的所有属性并将其与 foreach 等合并

The fastest way is to handle those properties in programming way - no with SQL.

You should grab all dogs, in the second request all properties for dogs you are interested in and merge it with foreach, etc

世界和平 2024-12-19 07:43:45

我将创建一个包含字段的临时表:

group_id
dog_id
dog_name
elmt_name
attr_value

这是原始数据库的平面表示。两件重要的事情:

1)该表必须使用 mysql 存储过程生成,而不是使用 php AND 在一个事务中生成。否则,如果你有数千只狗,速度会非常慢。

2) 这个平面表是您的报表的特殊缓存表。如果狗的数据变化缓慢,则无需删除它,直到狗的数据发生变化。

然后通过简单的查询就可以生成 Excel 导出。不要忘记索引!

I would create a temp table with fields:

group_id
dog_id
dog_name
elmt_name
attr_value

This is a flat representaion of the original database. Two important things:

1) this table must be generated with mysql stored procedure NOT with php AND in one transaction. Otherwise if you have thousands of dogs it will be very slow.

2) this flat table is a special cache table for your report. If dogs data are changed slowly, you do not need to delete it until changing dogs data.

Then with a simple query you can generate the excel export. Do not forget the indexes!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文