[英]Joining a variable number of tables based on column value
I have a few Models in my code which is modeled in my MySQL database in this structure: 我的代码中有一些模型,该模型在以下结构的MySQL数据库中建模:
Properties
+----+------+---------+
| id | name | address |
|----+------+---------|
| 1 | p1 | 123 st |
| 2 | p2 | 123 st |
| 2 | p3 | 123 st |
+----+------+---------+
Tenants (belongs to property)
+----+-------------+-------+
| id | property_id | suite |
|----+-------------+-------|
| 1 | 1 | s1 |
| 2 | 1 | s2 |
| 3 | 2 | s3 |
+----+-------------+-------+
Costs (can belong to property or tenants)
+----+--------------+-----------+--------------+
| id | parent_model | parent_id | name |
|----+--------------+-----------+--------------+
| 1 | property | 1 | gardening |
| 2 | property | 2 | construction |
| 3 | tenant | 1 | renovation |
+----+--------------+-----------+--------------+
Files (can belong to any model)
+----+--------------+-----------+--------------+
| id | parent_model | parent_id | name |
|----+--------------+-----------+--------------+
| 1 | property | 1 | file1.jpg |
| 2 | tenant | 2 | file2.pdf |
| 3 | costs | 3 | file3.doc |
+----+--------------+-----------+--------------+
As you can see from the table structure all models can be linked back to a property
record (either directly or via one or more intermediary tables). 从表结构可以看出,所有模型都可以链接回property
记录(直接或通过一个或多个中间表)。
In my code I want to write one query that will get the property.id
of a file
After looking over this question: Joining different tables based on column value I realized finding a "link" from a file
to a property
can be done via a few joins. 在我的代码中,我想编写一个查询来获取file
的property.id
。看完这个问题之后: 基于列值连接不同的表,我意识到可以通过以下方法找到从file
到property
的“链接”很少加入。
The number of joins needed is different based on whatever the parent_model
is. 所需的联接数根据parent_model
不同而不同。 For file.id
= 1 its a matter of joining in the properties
table. 对于file.id
= 1,这是加入properties
表的问题。 For file.id
= 3 we must join in costs
, tenants
, and properties
对于file.id
= 3,我们必须加入costs
, tenants
和properties
How should a query be written that can get a property.id
for all of my files
records? 如何编写可以获取我所有files
记录的property.id
的查询?
Edit: This would be a sample output: 编辑:这将是一个示例输出:
+---------+-------------+
| file_id | property_id |
|---------+-------------|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
+---------+-------------+
In this case all the files worked out to be associated with property_id
1 but this may not always be the case. 在这种情况下,所有文件都被确定为与property_id
1相关联,但并非总是如此。
I don't think there's a shortcut. 我认为没有捷径可走。 You need to traverse all the paths something along these lines 您需要沿着这些路线遍历所有路径
select -- property files
FileID, parent_id
from Files
where parent_model='property'
union
select -- property costs
FileID, c.parent_id
from
Files inner join costs C on Files.parent_id=c.id and c.parent_model='property'
where Files.parent_model='costs'
union
select -- tenant costs
FileID, t.parent_id
Files inner join costs C on Files.parent_id=c.id and c.parent_model='tenant'
inner join tenant t on t.id=c.parent_id
where Files.parent_model='costs'
.... etc.
ie just string together all the variations then UNION
即只是将所有变体串在一起,然后是UNION
You should be able to do this like so: 您应该可以这样做:
SELECT
P.id,
P.name,
P.address,
F.name,
...
FROM
Files F
LEFT OUTER JOIN Costs C ON
F.parent_model = 'Cost' AND C.id = F.parent_id
LEFT OUTER JOIN Tenants T ON
(F.parent_model = 'Tenant' AND T.id = F.parent_id) OR
(C.parent_model = 'Tenant' AND T.id = C.parent_id)
LEFT OUTER JOIN Properties P ON
(F.parent_model = 'Property' AND P.id = F.parent_id) OR
(C.parent_model = 'Property' AND P.id = C.parent_id) OR
(P.id = T.property_id)
Depending on your data, this might break down. 根据您的数据,这可能会分解。 I don't think that I like the table design in this case. 在这种情况下,我认为我不喜欢桌子设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.