简体   繁体   English

加入两个共享共同祖父母但不是父母的表

[英]JOIN two tables that share a common grandparent, but not parent

I'm trying to unpack a layout system that organizes images and text like so:我正在尝试解压缩一个组织图像和文本的布局系统,如下所示:

布局

With the challenge being that I need to (somehow) associate each text.content with both its entry AND the first images.filename that follows it.挑战在于我需要(以某种方式)将每个text.content与其条目和它后面的第一个images.filename相关联。 Essentially introduce second cousins.基本上介绍了第二代堂兄弟。

Ideally, I'd get something like:理想情况下,我会得到类似的东西:

| entry.id  | images.filename  | texts.content
|-----------|------------------|--------------
| 0         | img.jpg          | lorem  

I've previously been successful at associating the images with their entry using a series of LEFT JOIN s like so:我以前使用一系列LEFT JOIN成功地将图像与其条目相关联,如下所示:

SELECT entries.id, images.filename 
FROM images 
LEFT JOIN containers 
    ON images.container_id = containers.id 
LEFT JOIN layers 
    ON containers.layer_id = layers.id 
LEFT JOIN entries 
    ON layers.entry_id = entries.id

But this doesn't work when trying to connect the texts to images, because they aren't directly associated.但这在尝试将文本连接到图像时不起作用,因为它们没有直接关联。 You have to go up to the entry level to determine association, but how do you then keep track of best-candidate for closest image?您必须将 go 提高到入门级才能确定关联,但是您如何跟踪最接近图像的最佳候选?

Currently目前

I feel like I'm closing in on something that will at least associate the images and texts to the same entry with:我觉得我正在接近一些至少将图像和文本与同一个条目相关联的东西:

SELECT entries.id, images.filename, texts.content
FROM entries, images, texts
LEFT JOIN containers a 
    ON texts.container_id = a.id 
LEFT JOIN containers b
    ON images.container_id = b.id 
LEFT JOIN layers 
    ON containers.layer_id = layers.id 
LEFT JOIN entries 
    ON layers.entry_id = entries.id

Though this is still kicking back a Not unique table/alias: 'entries' for some reason.尽管出于某种原因,这仍然会退回Not unique table/alias: 'entries'

Thusfar I've tried the aliasing stuff suggested here (which doesn't really seem to apply) and began hacking at questions dealing with subqueries but... I've run out of search terms.到目前为止,我已经尝试了这里建议的别名内容(这似乎并不适用)并开始破解处理子查询的问题,但是......我已经用完了搜索词。

Is this even possible?这甚至可能吗?

To be clear, I'm not looking for a whole answer here, just some hints as to how I might use JOINs to do the following:需要明确的是,我不是在这里寻找完整的答案,只是关于如何使用 JOIN 执行以下操作的一些提示:

A) Display texts and images and their entry associations ( I think I'm close here.) A)显示文本和图像及其条目关联(我想我在这里很接近。)

B) Associate one text entry with data from whatever image entry follows it. B)将一个文本条目与来自它后面的任何图像条目的数据相关联。 . . This feels like an impossibility right now, but I guess that's why I'm asking people who know more than I do.现在这感觉像是不可能的,但我想这就是为什么我要问比我了解更多的人。

EDIT: Note that there can be multiple layers containing either images or texts and I need to return all texts from each entry.编辑:请注意,可以有多个图层包含图像或文本,我需要从每个条目返回所有文本。

looking to you schema seems you could try using a fake aggregation function based on a union for select filename and text看着你的架构似乎你可以尝试使用基于 select 文件名和文本的联合的假聚合 function

SELECT id, max(filename) filename, max(content) content 
FROM (
    SELECT entries.id, images.filename, NULL content
    FROM entries
        INNER JOIN layers 
            ON entries.id  = layers.entry_id
        INNER JOIN containers 
            ON containers.layer_id = layers.id
        INNER JOIN images 
            ON images.container_id = containers.id
    UNION ALL 
    SELECT entries.id, NULL, texts.content
    FROM entries
        INNER JOIN layers 
            on entries.id  = layers.entry_id
        INNER JOIN containers 
            on containers.layer_id = layers.id
        INNER JOIN texts 
        ON texts.container_id = containers.id ) t
GROUP BY id 

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

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