SQL 使用来自另一个 table 的条件结果的 ORDER BY

SQL ORDER BY with conditional results from another table

我有一个 WordPress post table 和一个 postmeta table。对于那些不熟悉结构的人,这里是:

wp_postmeta table:

+------------+---------------------+------+-----+---------+----------------+
| Field      | Type                | Null | Key | Default | Extra          |
+------------+---------------------+------+-----+---------+----------------+
| meta_id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| post_id    | bigint(20) unsigned | NO   | MUL | 0       |                |
| meta_key   | varchar(255)        | YES  | MUL | NULL    |                |
| meta_value | longtext            | YES  |     | NULL    |                |
+------------+---------------------+------+-----+---------+----------------+

wp_postmeta的示例数据:

+---------+---------+---------------------+-------------+
| meta_id | post_id | meta_key            | meta_value  |
+---------+---------+---------------------+-------------+
|   20399 |     267 | youtube_video_id_zh | xdr4lzJ4pYU |
|   20403 |     267 | video_view_count    | 3           |
|   21855 |     278 | video_view_count    | 13          |
|   21858 |     278 | youtube_video_id_zh | 5yS5cmA4-rg |
|   26042 |     354 | video_view_count    | 13          |
|   26138 |     354 | youtube_video_id_zh | iXWMThIohcM |
+---------+---------+---------------------+-------------+

wp_poststable:(ID为postID)

+------------------------+---------------------+------+-----+----------+----------------+
| Field                  | Type                | Null | Key | Default  | Extra          |
+------------------------+---------------------+------+-----+----------+----------------+
| ID                     | bigint(20) unsigned | NO   | PRI | NULL     | auto_increment |
| (other fields skipped) |                     |      |     |          |                |
+------------------------+---------------------+------+-----+----------+----------------+

每个 WordPress post 在 wp_postmeta 中可以有多个元行。我需要根据 meta_value 的值用 ORDER BYwp_posts table 上做一个 SELECT 但只有当 meta_keyvideo_view_count 以便我可以获得具有 highest/lowest 观看次数的 post。我不能使用简单的 JOIN 因为 WHERE 条件需要检查其他类型的 meta_key.

这在 (My)SQL 中可行吗?

首先,我们可以从 wp_posts 和 select post 数据加入 post 元数据。请注意,我们使用 group by 因为加入。

SELECT wp_posts.* FROM
wp_posts
LEFT JOIN wp_postmeta ON (wp_posts.id = wp_postmeta.post_id)
GROUP BY wp_posts.id

从这里我们只能加入适当的 post元并使用它来订购:

SELECT wp_posts.*, wp_postmeta.meta_value AS video_count FROM
wp_posts
LEFT JOIN wp_postmeta ON (wp_posts.id = wp_postmeta.post_id AND wp_postmeta.meta_key = 'video_view_count')
GROUP BY wp_posts.id
ORDER BY wp_postmeta.meta_value

(请注意,如果 meta_keys 未按 post 重复,则实际上不需要分组依据)

评论后编辑,在另一个 postmeta:

上添加排除
SELECT wp_posts.*, wp_postmeta.meta_value AS video_count 
FROM wp_posts
INNER JOIN wp_postmeta ex ON (wp_posts.id = ex.post_id  AND wp_ex.meta_key = 'video_duration' AND CAST(wp_ex.meta_value AS CHAR) >= '240')
LEFT JOIN wp_postmeta ON (wp_posts.id = wp_postmeta.post_id AND wp_postmeta.meta_key = 'video_view_count')
GROUP BY wp_posts.id
ORDER BY wp_postmeta.meta_value