0
我有一組表單查詢MySQL的整理hasAndBelongsToMany數據
- 文件
- 組
- 關鍵詞
他們的關係如下
- 文件具有與屬於許多組
- 文件有屬於多個關鍵字
我想創建一個選擇所有文件的查詢,爲每個組創建不同的記錄(因爲我需要按組來選擇),這也GROUP_CONCATs所有關鍵字成一個單一的領域。
背後的原因是我使用獅身人面像索引文件表,我需要它索引標題,內容和關鍵字。我也必須能夠按組進行過濾。
這是我到目前爲止有:
SELECT
Link.document_id AS id,
Link.group_id AS db_id,
Document.title,
Document.description,
Group.name AS db_name,
GROUP_CONCAT(distinct Keyword.`content` SEPARATOR ", ") as `keywords`
FROM `groups_documents` AS Link
LEFT JOIN `documents` AS `Document` ON (Document.id = Link.document_id)
LEFT JOIN `groups` AS `Group` ON (`Group`.id = Link.group_id)
LEFT JOIN `keywords_documents` AS `KLink` ON (`KLink`.`document_id` = `Link`.`document_id`)
LEFT JOIN `keywords` AS `Keyword` ON (`Keyword`.`id` = `KLink`.`keyword_id`)
,但它不工作,不幸的是,我只得到一個行。
謝謝。我剛剛完成了工作,來到這裏發佈它,然後我看到了這個。然後我不得不連接group.id,但這就是生活。 – GTF