2013-03-02 190 views
1

我有兩個表:MySQL如何從同一列中選擇多個值以使它們出現在不同的列中?

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

但是我不知道這是否是最好的方法來得到我想要的,我已經閱讀了有關左連接並在過去使用它,但不知道我是否可以在這裏使用它或優點是什麼。

歡迎任何反饋!

編輯:我終於使用了左加入,所以我不知道如何投票或關閉此主題。它有一點比我先寫到這裏比較複雜,反正這是我的最終查詢:

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

你可能想看看在轉動查詢。我在MySQL中對他們不太熟悉,所以我不能再幫忙。但谷歌搜索可以幫助。 – 2013-03-02 21:49:45

回答

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 
相關問題