一个MySQL查询中的两个独立请求

时间:2015-11-10 20:52:16

标签: mysql wordpress

我试图通过使用来自两个不同表格的MySQL查询从特定帖子中获取大量帖子和meta_value:

SELECT SUM(CASE WHEN post_type='updates' AND post_status='publish' THEN 1 ELSE 0 END) AS updates FROM wp_posts, 
LEFT JOIN meta_value FROM wp_postmeta WHERE meta_key=content_version AND post_id=11242 AS version

表wp_posts看起来像:

|    ID   |    title   | post_type |  post_status  |
----------------------------------------------------
|    32   | Post Title |  updates  |    publish    |

表wp_postmeta看起来像:

| post_id |     meta_key    | meta_value |
-----------------------------------------
|  11242  | content_version |      1     |

无论如何我无法取得成果。你能帮我吗?

2 个答案:

答案 0 :(得分:1)

SELECT SUM(CASE WHEN post_type='post' AND post_status='publish' THEN 1 ELSE 0 END) AS updates, meta_value AS VERSION FROM wp_posts,wp_postmeta WHERE meta_key='content_version' AND post_id=11242

更新了我的查询

答案 1 :(得分:1)

尝试以下方法:

SQL Fiddle

SELECT SUM(CASE WHEN aa.post_type = 'updates' AND aa.post_status = 'publish' THEN 1 ELSE 0 END) AS updates, bb.meta_value AS version
FROM wp_posts AS aa
INNER JOIN wp_postmeta AS bb
ON aa.ID = bb.post_id
WHERE bb.meta_key = '1' AND bb.post_id = 11242;

结果:

updates | version
2       | 1