从wordpress(woocommerce)

时间:2015-09-07 18:58:57

标签: mysql wordpress select woocommerce distinct

我正在努力:

  1. 从wp_posts中选择不同的(到ID)ID(帖子标识)和post_title,其中post_status = publish和post_type = product;
  2. 从wp_postmeta中选择distinct(to post_id)post_id和meta_value;
  3. 我需要用一个select语句来做这个,所以我尝试了尽可能多的我想象并搜索。我试过的最后一个就是这个:

    
        SELECT a.ID,a.post_title,b.post_id,b.meta_value FROM 
        (
            SELECT DISTINCT ID FROM wp_posts
            UNION
            SELECT DISTINCT post_id FROM wp_postmeta
        ) x LEFT JOIN
        wp_posts a ON x.ID = a.ID LEFT JOIN
        wp_postmeta b ON x.post_id = b.post_id
    
    

    但是,当我尝试时,它会告诉我:

    #1054 - 'on clause'中的未知列'x.post_id'

    我什么都没想到,有人会帮助我吗?

    ----编辑----

    @Rahul,

    这很好,但仍然返回非不同的值,我正在使用它,(注意,常见列:a.ID = yy.post_id):

        SELECT a.ID, a.post_title, b.post_id, b.meta_value FROM wp_posts a
        LEFT JOIN (
    
            SELECT DISTINCT ID
            FROM wp_posts
        )xx ON a.ID = xx.ID
        LEFT JOIN (
    
            SELECT DISTINCT post_id
            FROM wp_postmeta
        )yy ON a.ID = yy.post_id
        LEFT JOIN wp_postmeta b ON b.post_id = yy.post_id
        WHERE a.post_type = 'product'
        AND a.post_status = 'publish'
        LIMIT 0 , 30
    

    回归:

    ID POST_TITLE POST_ID meta_value

    141 Pastel de Teste 141 1

    141 Pastel de Teste 141 1436921168:1

    141 Pastel de Teste 141 123

    141 Pastel de Teste 141可见

0 个答案:

没有答案