我正在开发一个简单的 CMS 系统,我有一个包含以下表格的数据库:
Items
Contents
Langs
该Items
表具有以下结构:
itemId
name (for semantic reasons)
type
parent (foreign key to itemId)
一个项目可以是一个document
或一个section
类型。部分是文档上的一段内容,通过父列链接到它。但是一个文档也可以有一个父级,这使得它成为一个subpage
.
现在我陷入了查询以分层地从数据库中获取所有项目的问题。所以是这样的:
documentId => name
metaDescription => language => meta
sections => sectionId => language => title
content
uri
subPages => documentId => name
metaDescription
sections => etc...
澄清一下,一个网站可以在Langs
表格中有多种语言,并且每种语言都链接到表格中的一段内容,该内容Contents
也链接到Items
表格中的一个项目。metaDescription
是content
链接到类型项目的链接列document
。
有没有办法用一个查询来做到这一点?这是我的第一次尝试,但它不适用于子页面:
SELECT
documents.itemId AS id,
documents.name AS documentName,
documents.lastModified AS lastModified,
meta.content AS metaDescription,
meta.uri AS documentUri,
sections.itemId AS sectionId,
sections.name AS sectionName,
sections.lastModified AS sectionLastModified,
contents.name AS sectionTitle,
contents.content AS sectionContent,
contents.uri AS contentUri,
contents.lastModified AS contentLastModified,
langs.name AS contentLang
FROM
SITENAME_kw_items AS documents
INNER JOIN
SITENAME_kw_contents AS meta
ON documents.itemId = meta.itemId
INNER JOIN
SITENAME_kw_items AS sections
ON sections.parent = documents.itemId
INNER JOIN
SITENAME_kw_contents AS contents
ON sections.itemId = contents.itemId
INNER JOIN
SITENAME_kw_langs AS langs
ON langs.langId = contents.langId
对不起,很长的问题。希望大家能帮忙!