私は2つのテーブルを持っています:
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_posts.ID に属する wp_postmeta からいくつかのフィールドを取得するクエリが必要です。これまでのところ、私は思いついた:
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'