[英]MySQL: Two tables share common “date” column, how to ORDER dates together after a JOIN?
[英]JOIN two tables that share a common grandparent, but not parent
我正在嘗試解壓縮一個組織圖像和文本的布局系統,如下所示:
挑戰在於我需要(以某種方式)將每個text.content
與其條目和它后面的第一個images.filename
相關聯。 基本上介紹了第二代堂兄弟。
理想情況下,我會得到類似的東西:
| entry.id | images.filename | texts.content
|-----------|------------------|--------------
| 0 | img.jpg | lorem
我以前使用一系列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
但這在嘗試將文本連接到圖像時不起作用,因為它們沒有直接關聯。 您必須將 go 提高到入門級才能確定關聯,但是您如何跟蹤最接近圖像的最佳候選?
我覺得我正在接近一些至少將圖像和文本與同一個條目相關聯的東西:
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
盡管出於某種原因,這仍然會退回Not unique table/alias: 'entries'
。
到目前為止,我已經嘗試了這里建議的別名內容(這似乎並不適用)並開始破解處理子查詢的問題,但是......我已經用完了搜索詞。
需要明確的是,我不是在這里尋找完整的答案,只是關於如何使用 JOIN 執行以下操作的一些提示:
A)顯示文本和圖像及其條目關聯(我想我在這里很接近。)
B)將一個文本條目與來自它后面的任何圖像條目的數據相關聯。 . 現在這感覺像是不可能的,但我想這就是為什么我要問比我了解更多的人。
編輯:請注意,可以有多個圖層包含圖像或文本,我需要從每個條目返回所有文本。
看着你的架構似乎你可以嘗試使用基於 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.