简体   繁体   English

具有多种数据类型的闭包表?

[英]Closure table with multiple data types?

I've been brushing up on my MySQL lately and I need to make a database with hierarchical data. 我最近一直在刷我的MySQL,我需要创建一个包含分层数据的数据库。

I have several different types of data that need to be represented in a tree format, but don't know how to go about doing it. 我有几种不同类型的数据需要以树格式表示,但不知道如何去做。

For example, Let's say I have a person, who can employ, or be employed by other people. 例如,假设我有一个人,可以雇用或受雇于其他人。 Each of these people may have equipment checked out to them, and each piece of equipment must have a name, description, and a list of replacement parts, and each replacement part must have a cost, etc. etc. 这些人中的每一个都可能有设备检查,每件设备必须有名称,描述和更换部件清单,每个更换部件必须有成本等。

Most examples of closure tables I see focus on how awesome they are for handling forums, or threaded-comments. 我看到关闭表的大多数例子都集中在它们处理论坛或线程注释的方式上。 How do I go about making a closure table that has multiple data types? 如何制作具有多种数据类型的闭包表?

Here's a quick and dirty example: 这是一个快速而肮脏的例子:

select * from person

| pID | name      | employedBy |
+-----+-----------+------------+
|   1 | John Doe  |          2 |
|   2 | Joe Smith |       NULL |
|   3 | Meg Ryan  |          3 |

select * from equipment

| eqID | eqName   | eqDescription     | eqOwner | eqCheckedOutTo |
+------+----------+-------------------+---------+----------------+
|    1 | stuff    | just some stuff   |       3 |           NULL |
|    2 | table    | a table           |       1 |           NULL |
|    3 | computer | PC computer       |       3 |              2 |
|    4 | 3table   | table with 3 legs |       2 |           NULL |

select * from parts;

| partID | partName     | partCost |
+--------+--------------+----------+
|      1 | desktop1     |   499.99 |
|      2 | monitor13x13 |   109.95 |
|      3 | windows95    |    10.00 |
|      4 | speakers     |    30.00 |
|      5 | tabletop     |   189.99 |
|      6 | table leg    |    59.99 |

select * from equipmentParts

| epID | eqID | partID | quantity |
+------+------+--------+----------+
|    1 |    3 |      1 |        1 |
|    2 |    3 |      2 |        2 |
|    3 |    3 |      3 |        1 |
|    4 |    2 |      5 |        1 |
|    5 |    2 |      6 |        4 |
|    6 |    4 |      5 |        1 |
|    7 |    4 |      6 |        3 |

They you can query like: 他们可以查询如下:

select name,eqName,e.eqID,partName,partCost,quantity,(quantity*partCost) AS totCost
from person p
inner join equipment e ON e.eqOwner=p.pID
inner join equipmentParts ep ON ep.eqID=e.eqID
inner join parts pa ON ep.partID=pa.partID

| name      | eqName   | eqID | partName     | partCost | quantity | totCost |
+-----------+----------+------+--------------+----------+----------+---------+
| John Doe  | table    |    2 | tabletop     |   189.99 |        1 |  189.99 |
| John Doe  | table    |    2 | table leg    |    59.99 |        4 |  239.96 |
| Meg Ryan  | computer |    3 | desktop1     |   499.99 |        1 |  499.99 |
| Meg Ryan  | computer |    3 | monitor13x13 |   109.95 |        2 |  219.90 |
| Meg Ryan  | computer |    3 | windows95    |    10.00 |        1 |   10.00 |
| Joe Smith | 3table   |    4 | tabletop     |   189.99 |        1 |  189.99 |
| Joe Smith | 3table   |    4 | table leg    |    59.99 |        3 |  179.97 |

or summarize the cost of each equipment: 或总结每台设备的成本:

select name,eqName,sum(quantity*partCost) AS totCost
from person p
inner join equipment e ON e.eqOwner=p.pID
inner join equipmentParts ep ON ep.eqID=e.eqID
inner join parts pa ON ep.partID=pa.partID
group by e.eqID

| name      | eqName   | totCost |
+-----------+----------+---------+
| John Doe  | table    |  429.95 |
| Meg Ryan  | computer |  729.89 |
| Joe Smith | 3table   |  369.96 |

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

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