使用多个连接查询MySQL

时间:2017-09-19 09:01:24

标签: mysql

我使用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`, 

documentsnumeric_shelfmarkdocumentsshelfmarklocationname FROM(documents_revisions)加入collections ON documentscollection_id = collectionsid加入 documents_revisions documents_revisionsdocument_id = documentsid加入subprojects_x_documents ON documentsid = subprojects_x_documentsdocument_id在哪里 subprojects_x_documentssubproject_id ='stgall_lotterie'AND documents_revisionslatest = 1 ORDER BY locationnamedocumentssort_ordernumeric_shelfmark

我收到错误消息:“不唯一的表/别名:'documents_revisions'”

2 个答案:

答案 0 :(得分:0)

您的表格称为文档,而不是文档

答案 1 :(得分:0)

好的,经过一些调整后我得到了我的结果:

SELECT documents_revisionsdocument_iddocuments_revisionscollection_iddocuments_revisionsnumeric_shelfmarkdocuments_revisionsshelfmarklocationname FROM (documents_revisions)加入collections ON documents_revisionscollection_id = collectionsid加入 subprojects_x_documents documents_revisionsdocument_id = subprojects_x_documentsdocument_id在哪里 subprojects_x_documentssubproject_id ='stgall_lotterie'AND documents_revisionslatest = 1 ORDER BY locationnamenumeric_shelfmark