3

我有 2 个 Wordpress 数据库查询,第一个显示所有帖子 + 类别。第二个显示所有帖子+自定义字段。

我正在寻找的是列出所有帖子+类别+自定义字段

查询一:列出帖子+分类

SELECT ID,
post_title,

(SELECT group_concat(wp_terms.name separator ", ") FROM wp_terms
INNER JOIN wp_term_taxonomy on wp_terms.term_id = 
wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = 
wp_term_taxonomy.term_taxonomy_id
WHERE taxonomy="category" and wp_posts.ID = wpr.object_id ) AS 
"Categories"
FROM wp_posts WHERE post_type = "post" AND post_status = "publish"

+------+--------------------------------------+------------+
| ID   | post_title                           | Categories |
+------+--------------------------------------+------------+

查询2:列出帖子+自定义字段

SELECT ID, post_title, pm1.meta_value as "Amazon.com", pm2.meta_value 
as "Amazon.co.uk" FROM wp_posts
LEFT JOIN wp_postmeta pm1 ON ID = pm1.post_id AND 
pm1.meta_key = "Amazon.com"
LEFT JOIN wp_postmeta pm2 ON ID = pm2.post_id AND 
pm2.meta_key = "Amazon.co.uk"
WHERE post_type = "post" AND post_status = "publish"

+------+-----------------------+--------------+--------------+
| ID   | post_title            | Amazon.com   | Amazon.co.uk |
+------+-----------------------+--------------+--------------+

如何组合结果以便列出所有帖子 + 类别 + 自定义字段?

+------+-------------+-------------+--------------+--------------+
| ID   | post_title  | Categories  | Amazon.com   | Amazon.co.uk |
+------+-------------+-------------+--------------+--------------+
4

2 回答 2

3

尝试使用以下查询:

SELECT ID, post_title, pm1.meta_value as "Amazon.com", pm2.meta_value as "Amazon.co.uk" , 
                                        (SELECT group_concat(wp_terms.name separator ", ") FROM wp_terms
                                        INNER JOIN wp_term_taxonomy on wp_terms.term_id = 
                                        wp_term_taxonomy.term_id
                                        INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = 
                                        wp_term_taxonomy.term_taxonomy_id
                                        WHERE taxonomy="category" and wp_posts.ID = wpr.object_id ) AS "Categories"
FROM wp_posts
LEFT JOIN wp_postmeta pm1 ON ID = pm1.post_id AND 
pm1.meta_key = "Amazon.com"
LEFT JOIN wp_postmeta pm2 ON ID = pm2.post_id AND 
pm2.meta_key = "Amazon.co.uk"
WHERE post_type = "post" AND post_status = "publish"
于 2019-03-16T21:50:28.303 回答
0

为了获得相同结果中的所有列,是将两个查询“合并”在一起,SELECT并将每个查询中列出的唯一列。

这样的事情应该可以解决问题:

SELECT 
  ID,
  post_title,
  (SELECT 
     GROUP_CONCAT(wp_terms.name SEPARATOR ", ") 
   FROM 
     wp_terms
     INNER JOIN wp_term_taxonomy on wp_terms.term_id=wp_term_taxonomy.term_id
     INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id= wp_term_taxonomy.term_taxonomy_id
   WHERE 
     taxonomy="category"   
     AND wp_posts.ID=wpr.object_id 
  ) AS `Categories`,
  GROUP_CONCAT(pm1.meta_value) AS `Amazon.com`, 
  GROUP_CONCAT(pm2.meta_value) AS `Amazon.co.uk`
FROM 
  wp_posts 
  LEFT JOIN wp_postmeta AS `pm1` ON pm1.post_id=ID AND pm1.meta_key="Amazon.com"
  LEFT JOIN wp_postmeta AS `pm2` ON pm2.post_id=ID AND pm2.meta_key="Amazon.co.uk"
WHERE 
  post_type="post" 
  AND post_status="publish"
GROUP BY
  ID;

让我知道是否有任何问题。

希望这可以帮助,

于 2019-03-16T21:48:38.040 回答