2016-11-30 84 views
1

好久不見!SQL - 選擇需要填充的變量

我正在使用WordPress的我的網站有很多自定義post_type和多種分類系統與由Toolset plugin設置toxonomies。

我想從一個巨大的查詢存儲到MySQL表中的一些帖子,我想每天晚上cron增加我的網站的速度。

這裏是我做過什麼:

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想應該像這樣的「計數(wp_posts.ID)查詢結果」的「term_taxonomy_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 
... 
... 

我堅持:( 任何人有一個想法?

感謝。

回答

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`[email protected], `title`[email protected], `posts`[email protected], `cats`=cats 

不知道這可能會更好,但它的工作原理:)

詢問是否需要一些解釋:)