显示多个 MySQL 表中的数据
我有 14 个表(每年一个),其中包含产品代码、公司名称和发票号码。表的主要结构是相同的(产品代码、ID),但公司名称中可以有一些变量。
Table2011
| ID | productcode | firm1 | firm2 | firm3 | etc |
| 1 | G-00001 | 2;5;40| 32;67 | | 150 |
| 2 | G-00005 | | 50 | | |
|etc | | | | | |
Table2010
| ID | productcode | firm1 | firm2 | firm3 |etc |
| 1 | G-00001 | 1;10 | | 55 | |
| 2 | G-00003 | | 2 | | |
| 3 | G-00005 | | 50 | 40 | |
| etc| | | | | |
Table2009
...
列 Firm1 通常不等于其他表中的公司 1
我正在使用表编辑器来处理表(向表中添加列、编辑值...)。
我想知道是否有可能实现如下所示的结果。这超出了我的 PHP 技能。
产品G-00001页面
…
<UL>
<LI>Year 2011: 150etc; 67firm2; 40firm1; 32firm2; 5firm1; 2firm1</LI>
<LI>Year 2010: 55firm3; 10firm1; 1firm1</LI>
<LI>Year 2009: ...</LI>
...
</UL>
…
I have 14 tables (one for every year) with product code, firm name and invoice numbers. Main structure of table is identical (product code, ID), but there can be some variables in names of firms.
Table2011
| ID | productcode | firm1 | firm2 | firm3 | etc |
| 1 | G-00001 | 2;5;40| 32;67 | | 150 |
| 2 | G-00005 | | 50 | | |
|etc | | | | | |
Table2010
| ID | productcode | firm1 | firm2 | firm3 |etc |
| 1 | G-00001 | 1;10 | | 55 | |
| 2 | G-00003 | | 2 | | |
| 3 | G-00005 | | 50 | 40 | |
| etc| | | | | |
Table2009
...
Column Firm1 do not usually equals to same firm as firm 1 in other table
I am using table editor to work with tables (adding columns to table, editing values…).
I would like to know if it is possible to achieve result like below. It is above my PHP skills.
Product G-00001 page
…
<UL>
<LI>Year 2011: 150etc; 67firm2; 40firm1; 32firm2; 5firm1; 2firm1</LI>
<LI>Year 2010: 55firm3; 10firm1; 1firm1</LI>
<LI>Year 2009: ...</LI>
...
</UL>
…
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我从书籍推荐开始:SQL Antipatterns。你会需要它,不管是你造成了这个混乱还是只是被分配来修复它。
如果我处在你的位置,首先要做的就是修复数据库结构。这太疯狂了。您不需要每年都有一个新表,也不需要每家公司都有一个新列。数据库不是 Excel 电子表格的一种形式。
其中 PK - 主键,FK - 外键。
这种结构将使信息的收集变得更加容易。
Lemme begin with book recommendation : SQL Antipatterns. You will need it, doesn't matter if you caused this mess or ar just assigned to fix it.
If i was in your place, first thing would do would be to fix the database structure. This is madness. You do not need a new table for each year and new column for each company. Database is not a form of Excel spreadsheet.
Where PK - primary key and FK - foreign key.
This structure would make the gathering of information much much much MUCH easier.
如果您只想显示数据而不担心重组,您可以使用
JOIN
来显示所有表中的信息。虽然我同意 teresko 的观点,但你确实需要重新设计该数据库。它是不可维护的。
If you just want to display the data and not worry about the restructuring just yet you can use a
JOIN
to display the information from all the tables.Although I would agree with teresko you really need to redesign that database. It is not maintainable the way it is.