简体   繁体   English

MySQL查询从多个表中检索数据作为嵌套树

[英]MySQL query to retrieve data from multiple tables as nested tree

Found similar questions on SO, but nothing that helped. 在SO上发现了类似的问题,但没有任何帮助。 Here's my problem: 这是我的问题:

I have a table, 'events', that will be rather large. 我有一个表“ events”,它会很大。 Also there are these two tables: 'photos' and 'videos'. 另外,还有两个表格:“照片”和“视频”。 Every 'events' record has an eid (event id). 每个“事件”记录都有一个eid(事件ID)。 Every 'photos' and 'videos' record has an aeid (associated event id). 每个“照片”和“视频”记录都有一个aeid(关联的事件ID)。

To grab event data of say 20 events, I'm currently running PHP like so: 为了获取说20个事件的事件数据,我目前正在像这样运行PHP:

array = get top 20 events
for these 20 events
  array['photos'] = get all photos
  array['videos'] = get all videos
return events array

I'm finding out that this is running too slow. 我发现这运行太慢。 Probably because that equates to 41 mysql queries. 可能因为这等于41个mysql查询。 I realize I need use JOIN somehow (I think I do at least), but I can't figure our how to structure my query so that it nested the photos and videos in the same way that I'm doing it in the pseudocode above. 我意识到我需要以某种方式使用JOIN(至少我想这样做),但是我无法弄清楚我们如何构造查询,以便它以与上述伪代码相同的方式嵌套照片和视频。 。 Also, I don't think I have the time to spare to switch over to postgres (unless it is very highly suggested); 另外,我认为我没有时间来切换到postgres(除非强烈建议); I'm working with a large enough codebase for that to be a headache with only one dev (me). 我正在使用足够大的代码库,以至于只有一个开发人员(我)感到头疼。

I'm willing to read documents/manuals if you post them. 如果您张贴文件/手册,我愿意阅读。 But, I've already done so in a few places to no avail. 但是,我已经在一些地方这样做了,但无济于事。 Thanks! 谢谢!

Try something like.. 尝试类似..

 SELECT E.eid, V.aeid, P.aeid 
 FROM events E, videos V, photos P 
 WHERE E.eid='$ID' and V.aeid='$ID' and P.aeid='$ID' 
 ORDER BY E.eid desc

Untested, but you should get the point 未经测试,但您应该明白这一点

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

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