合并两个 select 语句(追加列)

combine two select statements (append columns)

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

为了获得相同结果中的所有列,需要 'merge' 将两个查询放在一起,并 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;

如果有任何问题,请告诉我。

希望这对您有所帮助,

尝试使用以下查询:

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"