SQL - 选择需要填充的变量

时间:2016-11-30 16:46:00

标签: mysql sql wordpress web

好久不见!

我在我的网站上使用wordpress,其中包含许多自定义post_type和多个类别系统,其中包含由Toolset plugin设置的toxonomies。

我试图将一些帖子存储到一个mysql表中,这个帖子来自一个巨大的查询,我希望每晚都能用来提高我网站的速度。

这是我做的:

SELECT wp_posts.ID as mid,wp_posts.post_title,wp_posts.post_name,(
    SELECT COUNT(wp_posts.ID) FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) LEFT JOIN wp_icl_translations t
    ON wp_posts.ID = t.element_id
    AND t.element_type = CONCAT('post_', wp_posts.post_type) WHERE 1=1 AND (
        ( wp_postmeta.meta_key = 'my_key' AND wp_postmeta.meta_value = mid )
        AND
        (
            ( mt1.meta_key = 'end_date' AND CAST(mt1.meta_value AS SIGNED) >= date_format(curdate(), '%Y%m%d') )
            OR
            ( mt1.meta_key = 'end_date' AND CAST(mt1.meta_value AS SIGNED) = '' )
        )
    ) AND wp_posts.post_type IN ('bp','code') AND ((wp_posts.post_status = 'publish'))
) AS countpost
FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'top-key' AND wp_postmeta.meta_value = 'yes' ) ) AND wp_posts.post_type = 'my_type' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title

它工作得很好,但我想添加一列!

columni想要看起来像"数组" " term_taxonomy_id" " COUNT(wp_posts.ID)查询结果"像这样:

SELECT group_concat(term_taxonomy_id) FROM `bpo_term_relationships` WHERE `object_id` = id_of_the_current_post_scanned_by_count_query

我的结果应该是这样的

bpo_posts.ID | bpo_posts.post_title | bpo_posts.post_name | COUNT(bpo_posts.ID) | "array" of "term_taxonomy_id"
1            | the title            | the-title           | 16                  | 815,712,1025
2            | hey you              | hey-you             | 5                   | 75,105,200
...
...

我卡住了:( 有人有想法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

我终于实现了我想做的事情!

以下是代码:

ALTER EVENT wp_mytable
ON SCHEDULE EVERY 1 DAY
STARTS '2016-12-02 01:00:00' 
DO
INSERT INTO wp_mytable (mid, name, title, cats, posts) 
    SELECT wp_posts.ID as mid, @titlex := wp_posts.post_title, @namex := wp_posts.post_name,(
        SELECT group_concat(DISTINCT term_taxonomy_id) FROM `wp_term_relationships` WHERE `object_id` IN (
            SELECT wp_posts.ID as idx FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) LEFT JOIN wp_icl_translations t
            ON wp_posts.ID = t.element_id
            AND t.element_type = CONCAT('post_', wp_posts.post_type) WHERE 1=1 AND (
                ( wp_postmeta.meta_key = 'my-key' AND wp_postmeta.meta_value = mid )
                AND
                (
                    ( mt1.meta_key = 'end_date' AND CAST(mt1.meta_value AS SIGNED) >= date_format(curdate(), '%Y%m%d') )
                    OR
                    ( mt1.meta_key = 'end_date' AND CAST(mt1.meta_value AS SIGNED) = '' )
                )
            ) AND wp_posts.post_type IN ('bp','code') AND ((wp_posts.post_status = 'publish'))
        )
    ) as cats,@countpostx := (
        SELECT COUNT(wp_posts.ID) FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) LEFT JOIN wp_icl_translations t
        ON wp_posts.ID = t.element_id
        AND t.element_type = CONCAT('post_', wp_posts.post_type) WHERE 1=1 AND (
            ( wp_postmeta.meta_key = 'my-key' AND wp_postmeta.meta_value = mid )
            AND
            (
                ( mt1.meta_key = 'end_date' AND CAST(mt1.meta_value AS SIGNED) >= date_format(curdate(), '%Y%m%d') )
                OR
                ( mt1.meta_key = 'end_date' AND CAST(mt1.meta_value AS SIGNED) = '' )
            )
        ) AND wp_posts.post_type IN ('bp','code') AND ((wp_posts.post_status = 'publish'))
    ) AS countpost
    FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'top-key' AND wp_postmeta.meta_value = 'yes' ) ) AND wp_posts.post_type = 'my_type' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title
ON DUPLICATE KEY 
UPDATE `name`=@namex, `title`=@titlex, `posts`=@countpostx, `cats`=cats

不确定这可能会更好但是有效:)

询问您是否需要一些解释:)