假设,我在 MySQL 数据库中有两个表。第一个名为products
,第二个是sales
.
让我为products
表格保留三个不同的条目....
+-------+-----------------+-----------+--------+
|id | productname | quantity | uom |
+-------+-----------------+-----------+--------+
|1 | water bottle | 20 | piece |
|2 | cable | 200 | meter |
|3 | soap | 1 | piece |
+-------+-----------------+-----------+--------+
我也在表格中输入了一些销售额sales
-
+-------+---------------------+-----------+--------+-----------+
|id | date | quantity | uom | productid |
+-------+---------------------+-----------+--------+-----------+
|1 | 2013-06-21 09:34:00 | 20 | meter | 2 |
|2 | 2013-06-21 10:35:00 | 100 | meter | 2 |
|3 | 2013-06-21 11:36:00 | 1 | piece | 3 |
+-------+---------------------+-----------+--------+-----------+
在上sales
表中productid
是foreign key
。每个销售条目都保持这一点productid
。现在,如何根据外键从左表中创建单个条目并从右表中创建多个条目的报表。
让我创建一个报告的模型-
+-----------------+-----------+--------+---------------------+-----------+
| productname | quantity | uom | date | quantity |
+-----------------+-----------+--------+---------------------+-----------+
| cable | 200 | meter | 2013-06-21 09:34:00 | 20 |
| | | | 2013-06-21 10:35:00 | 100 |
| soap | 1 | piece | 2013-06-21 11:36:00 | 1 |
| water bottle | 20 | piece | | |
+-----------------+-----------+--------+---------------------+-----------+
是否可以使用 MySQL 基于外键创建上述报告?