我通过优化器运行了这个查询。
SELECT *, chv_images.image_id FROM chv_images
LEFT JOIN chv_storages ON chv_images.image_storage_id = chv_storages.storage_id
LEFT JOIN chv_users ON chv_images.image_user_id = chv_users.user_id
LEFT JOIN chv_albums ON chv_images.image_album_id = chv_albums.album_id
LEFT JOIN chv_categories ON chv_images.image_category_id = chv_categories.category_id
LEFT JOIN chv_meta ON chv_images.image_id = chv_meta.image_id
WHERE chv_images.image_nsfw = 0 AND (chv_albums.album_privacy NOT IN ('private','private_but_link','custom') OR chv_albums.album_privacy IS NULL OR chv_albums.album_user_id IS NULL)
ORDER BY chv_images.image_id DESC
LIMIT 0,15
它看起来像这样,理论上从我理解的方面肯定更好。
(
SELECT
*,
chv_images.image_id
FROM
chv_images
LEFT JOIN
chv_albums
ON chv_images.image_album_id = chv_albums.album_id
WHERE
chv_images.image_nsfw = 0
AND (
chv_albums.album_user_id IS NULL
)
ORDER BY
chv_images.image_id DESC LIMIT 0,
15
)
UNION
DISTINCT (SELECT
*,
chv_images.image_id
FROM
chv_images
LEFT JOIN
chv_albums
ON chv_images.image_album_id = chv_albums.album_id
WHERE
chv_images.image_nsfw = 0
AND (chv_albums.album_privacy NOT IN ('private', 'private_but_link', 'custom')
OR chv_albums.album_privacy IS NULL)
ORDER BY
chv_images.image_id DESC LIMIT 0,
15)
ORDER BY
chv_images.image_id DESC LIMIT 0,
15
但是有一个问题。错误#1250 - 来自其中一个SELECT的表'chv_images'不能在全局ORDER子句中使用。
我尝试删除一些括号但是没有用。