1

我有两张桌子:

wp_posts

ID  post_title                      post_date
20  Título de tuit de ejemplo       2012-12-03 20:24:58
23  Título de teletipo de ejemplo   2012-12-04 13:56:33
25  Título de Clipping de ejemplo   2012-12-04 14:16:14
28  Título 4 diciembre 2012         2012-12-04 14:48:05
32  Título de noticia Actualidad    2012-12-04 17:35:57
78  Título de Destacado de ejemplo  2012-12-05 23:19:38
110 Título de Clipping radio        2012-12-09 15:57:28
112 Título de Clipping televisión   2012-12-09 15:59:31

wp_postmeta

meta_id  post_id  meta_key            meta_value
61       20       wpcf-custom-author  John Doe
134      28       wpcf-custom-author  John Smith
85       23       wpcf-custom-author     
111      25       wpcf-custom-author     
176      32       wpcf-custom-author  John Smith
614      110      wpcf-custom-author     
524      78       wpcf-custom-author     
627      112      wpcf-custom-author     
538      80       wpcf-custom-author     
48       20       wpcf-source    
132      28       wpcf-source         El País
83       23       wpcf-source         El País
109      25       wpcf-source         El País
174      32       wpcf-source         El País
612      110      wpcf-source         El Mundo

没错,这些是 wordpress 表,但我的问题是关于 mysql,我需要一个查询来从 wp_postmeta 检索属于同一 wp_posts.ID 的几个字段,以便我可以在同一行上获取所有信息。到目前为止,我想出了:

SELECT DISTINCT posts.post_title AS title,
posts.post_date AS publish_date, 
(SELECT subpostmeta.meta_value FROM wp_postmeta AS subpostmeta WHERE subpostmeta.post_id = postmeta.post_id AND subpostmeta.meta_key =  'wpcf-lead-in') AS lead_in,
(SELECT subpostmeta.meta_value FROM wp_postmeta AS subpostmeta WHERE subpostmeta.post_id = postmeta.post_id AND subpostmeta.meta_key =  'wpcf-custom-body') AS custom_body,
(SELECT subpostmeta.meta_value FROM wp_postmeta AS subpostmeta WHERE subpostmeta.post_id = postmeta.post_id AND subpostmeta.meta_key =  'wpcf-custom-author') AS custom_author,
(SELECT subpostmeta.meta_value FROM wp_postmeta AS subpostmeta WHERE subpostmeta.post_id = postmeta.post_id AND subpostmeta.meta_key =  'wpcf-source') AS source
FROM wp_posts AS posts, wp_postmeta AS postmeta
WHERE posts.ID = postmeta.post_id
AND posts.post_status = 'publish'
AND posts.post_type = 'contenido'
AND posts.post_date < NOW()
ORDER BY posts.post_date DESC

据我所知,这工作正常,因为我得到:

post_title                          post_date               lead_in                                                 custom_body                                             custom_author   source
Título de noticia de ejemplo        2013-02-04 15:10:25     En un lugar de la Mancha, de cuyo nombre no quiero...   En un lugar de la Mancha, de cuyo nombre no quiero...   Juan García     ABC
Título de Clipping televisión       2012-12-09 15:59:31                                                             Clipping TV: Your bones don't break, mine do. That...                   La Vanguardia
Título de Clipping radio            2012-12-09 15:57:28                                                             Clipping radio: Now that we know who you are, I kn...                   El Mundo
Título de Destacado de ejemplo      2012-12-05 23:19:38                                                             Really?! We'll go deliver this crate like professi...       

但是我不确定这是否是获得我想要的东西的最佳方法,我已经阅读过 LEFT JOIN 并在过去使用过它,但不确定我是否可以在这里使用它或者有什么优势。

欢迎任何反馈!

编辑:我终于使用了 LEFT JOIN 所以我不知道如何投票或关闭这个话题。它比我第一次在这里写的要复杂一些,无论如何这是我的最终查询:

    SELECT DISTINCT posts.*,
        posts.post_date                  AS publish_date,
        tbl_lead_in.meta_value           AS lead_in,
        tbl_custom_author.meta_value     AS custom_author,
        tbl_source.meta_value            AS source,
        tbl_category_terms.slug          AS category_term_slug,
        tbl_category_terms.term_id   AS category_term_id
    FROM $wpdb->posts AS posts
        LEFT JOIN $wpdb->postmeta AS tbl_lead_in
               ON ( posts.ID = tbl_lead_in.post_id
                    AND tbl_lead_in.meta_key = 'wpcf-lead-in' )
        LEFT JOIN $wpdb->postmeta AS tbl_custom_body ON
                  ( posts.ID = tbl_custom_body.post_id
                    AND tbl_custom_body.meta_key = 'wpcf-custom-body' )
        LEFT JOIN $wpdb->postmeta AS tbl_custom_author
               ON ( posts.ID = tbl_custom_author.post_id
                    AND tbl_custom_author.meta_key = 'wpcf-custom-author' )
        LEFT JOIN $wpdb->postmeta AS tbl_source
               ON ( posts.ID = tbl_source.post_id
                    AND tbl_source.meta_key = 'wpcf-source' )
        LEFT JOIN $wpdb->term_relationships AS tbl_category_rel
               ON (posts.ID = tbl_category_rel.object_id)
        RIGHT JOIN $wpdb->term_taxonomy AS tbl_category
               ON (tbl_category_rel.term_taxonomy_id = tbl_category.term_taxonomy_id
                    AND tbl_category.taxonomy = 'category')
        LEFT JOIN $wpdb->terms AS tbl_category_terms
               ON ( tbl_category_terms.term_id = tbl_category.term_id )
    WHERE posts.post_status = 'publish'
4

1 回答 1

1

您正在尝试透视将数据从行转换为列的数据。MySQL 没有数据透视函数,但您可以重写查询以使用聚合函数,并CASE为您返回的每个列使用一个表达式:

SELECT p.post_title,
  max(case when pm.meta_key = 'wpcf-lead-in' then pm.meta_value end) as lead_in,
  max(case when pm.meta_key = 'wpcf-custom-body' then pm.meta_value end) as custom_body,
  max(case when pm.meta_key = 'wpcf-custom-author' then pm.meta_value end) as custom_author,
  max(case when pm.meta_key = 'wpcf-source' then pm.meta_value end) as source
FROM wp_posts p
LEFT JOIN wp_postmeta pm
  ON p.id = pm.post_id
WHERE p.post_status = 'publish'
  AND p.post_type = 'contenido'
  AND p.post_date < NOW()
GROUP BY p.post_title
ORDER BY p.post_date DESC
于 2013-03-02T22:05:55.650 回答