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. 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:
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?
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.
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.
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:
A) Display texts and images and their entry associations ( I think I'm close here.)
B) Associate one text entry with data from whatever image entry follows it. . 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 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.