我使用MySQL 5.7并拥有以下表格:
documents:
---------------------------------------------------------------
id collection_id numeric_shelfmark shelfmark
---------------------------------------------------------------
1 kbt y024 Y 24
---------------------------------------------------------------
collections:
---------------------------------------------------------------
id name location
---------------------------------------------------------------
kbt kantonb thur Thurgau
gbt lorem ipsum
----------------------------------------------------------------
subprojects_x_documents
-----------------------------------------------------------------
subproject_id document_id
-----------------------------------------------------------------
foo 1
-----------------------------------------------------------------
documents_revisions
-----------------------------------------------------------------
document_id updated_at numeric_shelfmark shelfmark latest
-----------------------------------------------------------------
1 2016-06-22 10:32:01 y024 Y 24 0
1 2017-09-19 09:19:17 z024 Z 24 1
------------------------------------------------------------------
通过以下SELECT
查询,我可以设法获得与子项目和集合相关联的文档列表:
SELECT `documents`.`id`, `documents`.`collection_id`,
`documents`.`numeric_shelfmark`, `shelfmark`, `location`, `name`
FROM (`documents`)
JOIN `collections` ON `documents`.`collection_id` = `collections`.`id`
JOIN `subprojects_x_documents` ON `documents`.`id` =
`subprojects_x_documents`.`document_id`WHERE
`subprojects_x_documents`.`subproject_id` = 'stgall_lotterie'
ORDER BY `location`, `name`, `documents`.`sort_order`, `numeric_shelfmark`
现在,我正试图获取最新的(“documents_revisions.latest”= 1)文档修订版:
SELECT `documents`.`id`, `documents`.`collection_id`,
`documents`.`numeric_shelfmark`, `documents`.`shelfmark`, `location`,
name`FROM (`documents`)JOIN `collections` ON
`documents`.`collection_id` = `collections`.`id`JOIN
`documents_revisions` ON `documents_revisions`.`document_id` =
`documents`.`id`JOIN `subprojects_x_documents` ON `documents`.`id` =
`subprojects_x_documents`.`document_id` WHERE
`subprojects_x_documents`.`subproject_id` = 'stgall_lotterie' AND
`documents_revisions`.`latest` = 1 ORDER BY `location`, `name`,
`documents`.`sort_order`, `numeric_shelfmark`
但是此查询返回与以前相同的文档列表,并忽略documents_revisions.latest“= 1 part。
当我更新查询以对documents_revisons表而不是文档表执行操作时:
SELECT `documents`.`id`, `documents`.`collection_id`,
documents
。numeric_shelfmark
,documents
。shelfmark
,location
,
name
FROM(documents_revisions
)加入collections
ON
documents
。collection_id
= collections
。id
加入
documents_revisions
documents_revisions
。document_id
=
documents
。id
加入subprojects_x_documents
ON documents
。id
=
subprojects_x_documents
。document_id
在哪里
subprojects_x_documents
。subproject_id
='stgall_lotterie'AND
documents_revisions
。latest
= 1 ORDER BY location
,name
,
documents
。sort_order
,numeric_shelfmark
我收到错误消息:“不唯一的表/别名:'documents_revisions'”
答案 0 :(得分:0)
您的表格称为文档,而不是文档
答案 1 :(得分:0)
好的,经过一些调整后我得到了我的结果:
SELECT documents_revisions
。document_id
,
documents_revisions
。collection_id
,
documents_revisions
。numeric_shelfmark
,
documents_revisions
。shelfmark
,location
,name
FROM
(documents_revisions
)加入collections
ON
documents_revisions
。collection_id
= collections
。id
加入
subprojects_x_documents
documents_revisions
。document_id
=
subprojects_x_documents
。document_id
在哪里
subprojects_x_documents
。subproject_id
='stgall_lotterie'AND
documents_revisions
。latest
= 1 ORDER BY location
,name
,
numeric_shelfmark