无法在查询结尾处连接表,仅在某个部分中

时间:2014-06-02 19:58:27

标签: mysql sql

两者有什么区别,为什么我能用第一段代码加入表?第二个版本说有一个sql语法错误。

SELECT DISTINCT albums.id, albumPublishes.album_title AS title, albumPublishes.album_uuid AS guid, albumPublishes.album_description AS description,publishSchemas.created_at AS createdAt, publishSchemas.updated_at AS updatedAt, publishSchemas.id AS publishId,publishSchemas.note_publish_uuid AS notePublishUUID 
FROM production.publish_schemas AS publishSchemas 
LEFT JOIN production.album_publishes AS albumPublishes ON albumPublishes.visibility = 'Public' AND publishSchemas.album_publish_uuid = albumPublishes.album_publish_uuid 

INNER JOIN production.album_users

LEFT JOIN production.albums AS albums ON albums.album_id = albumPublishes.album_uuid 
WHERE albums.deleted_at IS NULL AND albumPublishes.album_title IS NOT NULL 
AND publishSchemas.publish_environment = 'Staging' HAVING guid != '**' AND guid != '***'

这是标记错误的版本:

SELECT DISTINCT albums.id, albumPublishes.album_title AS title, albumPublishes.album_uuid AS guid, albumPublishes.album_description AS description,publishSchemas.created_at AS createdAt, publishSchemas.updated_at AS updatedAt, publishSchemas.id AS publishId,publishSchemas.note_publish_uuid AS notePublishUUID 
FROM production.publish_schemas AS publishSchemas 
LEFT JOIN production.album_publishes AS albumPublishes ON albumPublishes.visibility = 'Public' AND publishSchemas.album_publish_uuid = albumPublishes.album_publish_uuid 


LEFT JOIN production.albums AS albums ON albums.album_id = albumPublishes.album_uuid 
WHERE albums.deleted_at IS NULL AND albumPublishes.album_title IS NOT NULL 
AND publishSchemas.publish_environment = 'Staging' HAVING guid != '**' AND guid != '***'

INNER JOIN production.album_users

编辑 - 这是我想要的查询,但我没有'我想我可以为尚未定义的东西注入一个参考:

SELECT DISTINCT albums.id, albumPublishes.album_title AS title, albumPublishes.album_uuid AS guid, albumPublishes.album_description AS description,publishSchemas.created_at AS createdAt, publishSchemas.updated_at AS updatedAt, publishSchemas.id AS publishId,publishSchemas.note_publish_uuid AS notePublishUUID 
    FROM production.publish_schemas AS publishSchemas 
    LEFT JOIN production.album_publishes AS albumPublishes ON albumPublishes.visibility = 'Public' AND publishSchemas.album_publish_uuid = albumPublishes.album_publish_uuid 

    LEFT JOIN production.album_users as albumUsers on albumUsers.album_id = albums.album_id

    LEFT JOIN production.albums AS albums ON albums.album_id = albumPublishes.album_uuid 
    WHERE albums.deleted_at IS NULL AND albumPublishes.album_title IS NOT NULL 
    AND publishSchemas.publish_environment = 'Staging' HAVING guid != '**' AND guid != '***'

2 个答案:

答案 0 :(得分:2)

SQL对查询中子句的顺序非常严格。必须是:

SELECT ...
FROM ...
JOIN ... JOIN ... JOIN ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
LIMIT ...

(我已经省略了一些不常见的条款。)大多数条款都是可选的,但是当它们被使用时,它们必须位于此排序的适当位置。

您尝试执行的查询应为:

SELECT DISTINCT albums.id, albumPublishes.album_title AS title, albumPublishes.album_uuid AS guid, albumPublishes.album_description AS description,publishSchemas.created_at AS createdAt, publishSchemas.updated_at AS updatedAt, publishSchemas.id AS publishId,publishSchemas.note_publish_uuid AS notePublishUUID 
FROM production.publish_schemas AS publishSchemas 
LEFT JOIN production.album_publishes AS albumPublishes ON albumPublishes.visibility = 'Public' AND publishSchemas.album_publish_uuid = albumPublishes.album_publish_uuid 
LEFT JOIN production.albums AS albums ON albums.album_id = albumPublishes.album_uuid 
LEFT JOIN production.album_users as albumUsers on albumUsers.album_id = albums.album_id
WHERE albums.deleted_at IS NULL AND albumPublishes.album_title IS NOT NULL 
AND publishSchemas.publish_environment = 'Staging' HAVING guid != '**' AND guid != '***'

您只需要订购所有联接,以便其ON子句仅引用先前FROMJOIN子句中的表。我交换了最后两个LEFT JOIN子句的顺序。

答案 1 :(得分:0)

联接必须在before子句中,检查这一个代码

    SELECT DISTINCT albums.id, albumPublishes.album_title AS title, albumPublishes.album_uuid AS guid, albumPublishes.album_description AS description,publishSchemas.created_at AS createdAt, publishSchemas.updated_at AS updatedAt, publishSchemas.id AS publishId,publishSchemas.note_publish_uuid AS notePublishUUID 
    FROM production.publish_schemas AS publishSchemas 

     LEFT JOIN production.album_publishes AS albumPublishes ON albumPublishes.visibility = 'Public' AND publishSchemas.album_publish_uuid = albumPublishes.album_publish_uuid 

    LEFT JOIN production.albums AS albums ON albums.album_id = albumPublishes.album_uuid 

    INNER JOIN production.album_users

    WHERE albums.deleted_at IS NULL AND albumPublishes.album_title IS NOT NULL 
    AND publishSchemas.publish_environment = 'Staging' HAVING guid != '**' AND guid != '***'