简体   繁体   English

根据第一个表合并来自多个表的数据

[英]Combining data from multiple tables based on the first table

I have a table full of items, at the moment there is only one type and everything about that item is in the same table. 我有一个充满项目的表,目前只有一种类型,关于该项目的所有内容都在同一个表中。 Now I want to add two more types. 现在,我想再添加两种类型。 A number of columns (>10) will be unique to each type, so I don't just want to have 30 columns in the table when 20 of them will be null values, so I'm thinking of splitting them up: 每种类型都有许多列(> 10)是唯一的,所以当它们中有20个为空值时,我不只是希望表中有30列,所以我想将它们拆分:

tbl_items_common
tbl_items_type1 [for data unique to this type]
tbl_items_type2 [for data unique to this type]
tbl_items_type3 [for data unique to this type]

All the tables will have a common field 'id', for matching up the items and data. 所有表都将具有一个公共字段“ id”,用于匹配项和数据。

Now I'm thinking about the coding and thinking: 现在我正在考虑编码和思考:

Should I bother splitting these up at all, or should I just have one big table. 我应该烦扰这些事情,还是我只有一张大桌子。 Makes inserting and querying easier, but the db is a bit more 'messy'. 使插入和查询更加容易,但是数据库有点“混乱”。

Splitting the data up makes the db a bit more isolated, but makes code more complicated. 拆分数据会使数据库更加孤立,但会使代码更复杂。

Or, I could just use 3 completely separate tables with the some of the columns the same between them, but then I'm not sure how to generate a unique id that is unique over all 3 tables. 或者,我可以只使用3个完全独立的表,并且其中一些列之间是相同的,但是我不确定如何生成在所有3个表中都是唯一的唯一ID。

If I do decide on splitting the data up, is it possible to get all the data in one query (embedded case/select maybe?), or would I have to do a SELECT first to find the item type and then another to grab the rest of the data? 如果我决定拆分数据,是否有可能在一个查询中获取所有数据(可能是嵌入式案例/选择?),还是我必须先执行SELECT才能找到项目类型,然后再进行另一个选择来获取其余数据? Not pretty when it comes to reporting. 就报告而言,这不是很漂亮。

So what do I want to know? 那我想知道什么? (1) Which method would you choose and (2) If you choose the second, how would you query that data? (1)您将选择哪种方法,(2)如果您选择第二种方法,将如何查询该数据?

You have a few options. 您有几种选择。 Personally, I'd keep all the important (and similar) data for your items in one table, and have 3 columns containing detail IDs, you could then create a couple of different tables containing the extra data for each type and JOIN that data to each query. 就个人而言,我会保持所有的项目重要的(类似)的数据在一个表中,并有详细的包含的ID 3列,然后你可以创建一个包含几个额外的数据为每种类型的不同表和JOIN这些数据来每个查询。

Another option is you could have three separate tables containing items, but I don't advise that as it's not good database design practice. 另一个选择是,您可以有三个单独的包含项目的表,但是我不建议这样做,因为这不是很好的数据库设计实践。 If you did want to do that though, you could either select from all three tables in one query: SELECT * FROM table1, table2, table3 如果您确实想这样做,则可以在一个查询中从所有三个表中进行选择:SELECT * FROM table1,table2,table3

... or manually match the column count using null values and perform a UNION query: ...或使用空值手动匹配列数并执行UNION查询:

SELECT f1, f2, f3, f4, null as f5 FROM table1
UNION ALL
SELECT f1, f2, f2, null as f4, null as f5 FROM table2
UNION ALL
SELECT f1, f2, f3, f4, f5 FROM table3 -- table 3 contains 5 columns as an example

... but that could get messy as well if you have lots of columns unique to each type. ...但是如果每种类型都有很多唯一的列,那也会变得混乱。 I'd suggest going with three separate tables for each item type's details : 我建议针对每个项目类型的详细信息使用三个单独的表:

SELECT * FROM items I
LEFT JOIN item_type1 I1 ON I.item1_detail_id = I1.id
LEFT JOIN item_type2 I2 ON I.item2_detail_id = I2.id
LEFT JOIN item_type3 I3 ON I.item3_detail_id = I3.id

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

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