我正在尝试查询多个WordPress表,我一直在学习。
这是我到目前为止所拥有的
SELECT
tr.object_id,
tr.term_taxonomy_id,
p.ID,
p.post_date,
p.post_title,
p.post_excerpt,
p.guid,
t.term_id,
t.name,
tt.taxonomy
FROM
wp_116_term_relationships AS tr,
wp_116_posts AS p,
wp_116_terms AS t LEFT JOIN
wp_116_term_taxonomy as tt ON tt.term_id = t.term_id
WHERE
p.post_type = 'post'
AND p.ID = tr.object_ID
AND tr.term_taxonomy_id = tt.term_taxonomy_id
AND p.post_date > '2013-06-01'
这是我得到的(对不起,我无法弄清楚如何发布这个清洁工 - 希望它有意义)
object_id term_taxonomy_id ID post_date post_title post_excerpt guid term_id name taxonomy
2356 33 2356 2013-06-07 15:56:54 Test Post for Reports http://domain.com/?p=2356 7496 Marketing Updates category
2356 32 2356 2013-06-07 15:56:54 Test Post for Reports http://domain.com/?p=2356 470 News Updates category
2356 70 2356 2013-06-07 15:56:54 Test Post for Reports http://domain.com/?p=2356 46408 Tag Test 1 post_tag
2356 72 2356 2013-06-07 15:56:54 Test Post for Reports http://domain.com/?p=2356 46410 Tag Test 2 post_tag
2356 74 2356 2013-06-07 15:56:54 Test Post for Reports http://domain.com/?p=2356 46412 Tag Test 3 post_t
如何将数据与名称字段分开,以便如果它是post_tag,则它位于一列(post_tag)中,如果它是一个类别,则它位于另一列(类别)中?例如:
object_id term_taxonomy_id ID post_date post_title post_excerpt guid term_id post_tag category
2356 33 2356 2013-06-07 15:56:54 Test Post for Reports http://domain.com/?p=2356 7496 Marketing Updates Tag Test 1
答案 0 :(得分:3)
使用CASE声明应该相当直接
SELECT
tr.object_id,
tr.term_taxonomy_id,
p.ID,
p.post_date,
p.post_title,
p.post_excerpt,
p.guid,
t.term_id,
CASE WHEN tt.taxonomy = 'category' THEN t.name ELSE NULL END AS category_name,
CASE WHEN tt.taxonomy = 'post_tag' THEN t.name ELSE NULL END AS post_tag_name
FROM
wp_116_term_relationships AS tr,
wp_116_posts AS p,
wp_116_terms AS t
LEFT JOIN wp_116_term_taxonomy as tt ON tt.term_id = t.term_id
WHERE
p.post_type = 'post'
AND p.ID = tr.object_ID
AND tr.term_taxonomy_id = tt.term_taxonomy_id
AND p.post_date > '2013-06-01'