mercredi 30 novembre 2016

SQL - Select a variable that needs to be populated

Long time no see !

I'm using wordpress for my site with lots of custom post_type and multiple category system with toxonomies set by the Toolset plugin.

I'm trying to store some posts into a mysql table from a huge query that i want to cron each night to increase speed of my site.

Here is what I did :

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

It work great but i want to add a column !

The columni want should look like an "array" of "term_taxonomy_id" of the "COUNT(wp_posts.ID) query result" like this :

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

my result should look like this

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
...
...

I'm stuck :( Anybody has an idea ?

Thanks.




Aucun commentaire:

Enregistrer un commentaire