2013-11-02 64 views
0

我正在开发一个应用程序,我基本上将部分wordpress数据库同步到应用程序。我希望不是创建wordpress的整个表结构,而是创建一个CategoryRelationship表,其中包含2列(POST_ID)和(CATEGORIES_ID)。类别ID是帖子所属的所有类别(包括父类别)的逗号分隔值。请注意我的wordpress数据库是多站点的一部分,因此是WP_2_前缀。如何在wordpress中查询所有帖子及其所属类别(包括父类别)的列表

尝试1)是以下几乎工作,并给我最接近我想要的结果,但似乎没有得到第一个父母(请注意我只有最高层次为3父母:

SELECT DISTINCT ID, 
(SELECT CONCAT_WS(',',group_concat(tt.term_id separator ','),tt.parent,tt2.term_id, tt3.term_id, tt4.term_id) 
FROM wp_2_terms 
JOIN wp_2_term_taxonomy tt on wp_2_terms.term_id = tt.term_id 
JOIN wp_2_term_relationships wpr on wpr.term_taxonomy_id = tt.term_taxonomy_id 
LEFT JOIN wp_2_term_taxonomy tt2 on tt.parent = tt2.term_id 
LEFT JOIN wp_2_term_taxonomy tt3 on tt2.parent = tt3.term_id 
LEFT JOIN wp_2_term_taxonomy tt4 on tt3.parent = tt4.term_id 
WHERE tt.taxonomy != 'category' and wp_2_posts.ID = wpr.object_id 
) AS 'Categories' 
FROM wp_2_posts 
WHERE post_type = 'post' 
AND post_status = 'publish' 
AND post_author = 2 

这让我像一个结果:

ID Categories 
10 21,101,166,183,311,350,356,357,360,363,0 
19 69,123,166,352,354,356,132,358,360,362,68,68 

上面的例子大多工作,虽然我错过了第一个家长,我知道这个查询可以给我重复,但我清理一下以后(如果我可以做我t all in 1 go显然是最好的)。
另外,如果我改变行:

LEFT JOIN wp_2_term_taxonomy tt2 on tt.parent = tt2.term_id 

TO

JOIN wp_2_term_taxonomy tt2 on tt.parent = tt2.term_id 

给我的结果,如:

ID Categories 
10 101,93,93 
19 69,123,68,68 

注意,我得到类别93现在的ID是10(我失踪在第一个),但不是其余。我已经尝试了各种不同的连接,但无济于事...

尝试2)我修改这个代码here,这让我的类别和父类,但我必须指定ID。我不知道如何在上面插入我的代码来循环获取所有类别的帖子ID。目前,它只是让我所有的父类& term_id的类别= 165

SELECT group_concat(T2.term_id separator ',') AS 'Cats' 
FROM (
    SELECT 
     @r AS _id, 
     (SELECT @r := parent FROM wp_2_term_taxonomy WHERE term_id = _id) AS parent, 
     @l := @l + 1 AS lvl 
    FROM 
     (SELECT @r := 165, @l := 0) vars, 
     wp_2_term_taxonomy tt 
    WHERE @r <> 0) T1 
JOIN wp_2_term_taxonomy T2 
ON T1._id = T2.term_id 
ORDER BY T1.lvl DESC; 

这让我像一个结果:

Cats 
165,142 

尝试3)这个查询仍然是很基本的,把我所有的类别和第一个父类放在一个单独的列中,但是我需要遍历父类别的一些方法......任何想法?我希望尝试2和3之间的组合可以工作,但我一直在这方面工作太久。

SELECT DISTINCT ID, TT.term_id, TT.parent 
FROM wp_2_posts p 
JOIN wp_2_term_relationships TR 
ON TR.object_id = p.ID 
JOIN wp_2_term_taxonomy TT 
ON TR.term_taxonomy_id = TT.term_taxonomy_id 
WHERE p.post_type = 'post' 
AND p.post_status = 'publish' 
AND p.post_author = 2 

这给了我一个表看起来像:

ID term_id parent 
10 1 0 
10 21 0 
10 101 93 
10 166 0 
10 183 0 
10 311 0 
10 350 0 
10 356 0 
10 357 0 
10 360 0 
10 363 0 
19 1 0 
19 69 68 
19 123 122 
19 166 0 
19 352 0 
19 354 0 
19 356 0 
19 132 0 
19 358 0 
19 360 0 
19 362 0 

预先感谢任何帮助。

回答

1

所以下面的查询适合我,虽然我不觉得这是最好的解决方案。我不得不补充说,每次连接的分类都是一样的。出于某种原因,当它在wp_2_term_taxonomy表上进行了第3次连接时,它加入了无效值......无法解释。希望有人有更好的解决方案。

SELECT DISTINCT ID, (
SELECT CONCAT_WS(',', GROUP_CONCAT(t.term_id 
SEPARATOR ',') , GROUP_CONCAT(DISTINCT tt2.term_id 
SEPARATOR ',') , GROUP_CONCAT(DISTINCT tt3.term_id 
SEPARATOR ',') , GROUP_CONCAT(DISTINCT tt4.term_id 
SEPARATOR ',')) 
FROM wp_2_terms t 
JOIN wp_2_term_taxonomy tt ON t.term_id = tt.term_id 
JOIN wp_2_term_relationships wpr ON wpr.term_taxonomy_id = tt.term_taxonomy_id 
LEFT JOIN wp_2_term_taxonomy tt2 ON tt.parent = tt2.term_id 
AND tt2.taxonomy = tt.taxonomy 
LEFT JOIN wp_2_term_taxonomy tt3 ON tt2.parent = tt3.term_id 
AND tt2.taxonomy = tt3.taxonomy 
LEFT JOIN wp_2_term_taxonomy tt4 ON tt3.parent = tt4.term_id 
AND tt3.taxonomy = tt4.taxonomy 
WHERE tt.taxonomy != 'category' 
AND wp_2_posts.ID = wpr.object_id 
) AS 'Categories' 
FROM wp_2_posts 
WHERE post_type = 'post' 
AND post_status = 'publish' 
AND post_author =2 
相关问题