合并两个选择语句(附加列)

时间:2019-03-16 21:39:11

标签: mysql sql wordpress wp-cli

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

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

查询1:列出帖子和类别

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 |
+------+-------------+-------------+--------------+--------------+

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分: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;

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

希望这会有所帮助,