1

我正在尝试创建一个自定义SELECT查询,以从 WordPress 自定义类型中找到我的个人最佳比赛,其中时间是自定义元字段。

这是我用来选择帖子的查询。我已经简化了它以进行调试,但最终我想选择几个术语 slug 并找到MIN每个 using GROUP BY

        SELECT      t.slug,
                    p.ID, 
                    p.post_date,
                    p.post_title,                    
                    m.meta_value as pb_mins
        FROM        $wpdb->posts p,
                    $wpdb->postmeta m,
                    $wpdb->term_relationships tr,
                    $wpdb->term_taxonomy tt,
                    $wpdb->terms t
        WHERE       p.ID = tr.object_id
                AND p.ID = m.post_id
                AND tr.term_taxonomy_id = tt.term_taxonomy_id
                AND tt.term_id = t.term_id
                AND p.post_type = 'runs'
                AND m.meta_key = 'ssr_duration_min'
                AND t.slug in ('10mi')

(我将结果输出到一个数组并print_r用于临时检查它。)

这将选择两个带有“10mi”术语的比赛/跑步,并为它们显示正确的时间/日期/标题。

当我添加MIN函数时问题来了:

        SELECT      t.slug,
                    p.ID, 
                    p.post_date,
                    p.post_title,                    
                    MIN(m.meta_value) as pb_mins

选择了正确的(最短)时间,但其他详细信息(日期、标题)来自其他 10 英里的跑步/比赛。

我尝试更改FROM要使用的子句,LEFT JOININNER JOIN同样的事情发生了:选择两个“10mi”运行都很好,但是当我添加MIN函数时会给出错误的输出。

谢谢你尽你所能的帮助。这是我在这里的第一篇文章,所以如果我需要添加更多细节,请告诉我。

4

2 回答 2

1

我从来没有来得及发布我的解决方案。现在已经有一段时间了,所以我什至不完全记得我是如何达到它的(很可能是反复试验),但如果这对任何人有帮助,欢迎他们。如果我想到更多细节,我会将它们作为评论添加到这个答案中。

            SELECT      p1.ID,
                        m1.meta_value,
                        t1.name
            FROM        $wpdb->posts p1,
                        $wpdb->postmeta m1,
                        $wpdb->term_relationships tr1,
                        $wpdb->term_taxonomy tt1,
                        $wpdb->terms t1,
                        (   SELECT      t2.slug as pb_distance,
                                        MIN(m2.meta_value) as pb_duration
                            FROM        $wpdb->posts p2,
                                        $wpdb->postmeta m2,
                                        $wpdb->term_relationships tr2,
                                        $wpdb->term_taxonomy tt2,
                                        $wpdb->terms t2
                            WHERE       p2.ID = tr2.object_id
                                    AND p2.ID = m2.post_id
                                    AND tr2.term_taxonomy_id = tt2.term_taxonomy_id
                                    AND tt2.term_id = t2.term_id
                                    AND p2.post_type = 'runs'
                                    AND p2.post_status = 'publish'
                                    AND m2.meta_key = 'ssr_duration_min'
                                    AND t2.slug IN $slugs
                            GROUP BY    t2.slug ) pb
            WHERE       p1.ID = m1.post_id
                    AND p1.ID = tr1.object_id
                    AND tr1.term_taxonomy_id = tt1.term_taxonomy_id
                    AND tt1.term_id = t1.term_id
                    AND p1.post_type = 'runs'
                    AND p1.post_status = 'publish'
                    AND m1.meta_key = 'ssr_duration_min'
                    AND t1.slug in $slugs
                    AND m1.meta_value = pb.pb_duration
            ORDER BY    meta_value+0
于 2012-06-15T14:30:41.127 回答
0

我使用的另一个选项也适用于稍微不同的查询,如下所示:

        SELECT      p.ID,
                    m.meta_value,
                    t.name
        FROM        $wpdb->posts p,
                    $wpdb->postmeta m,
                    $wpdb->term_relationships tr,
                    $wpdb->term_taxonomy tt,
                    $wpdb->terms t
        WHERE       p.ID = m.post_id
                AND p.ID = tr.object_id
                AND tr.term_taxonomy_id = tt.term_taxonomy_id
                AND tt.term_id = t.term_id
                AND p.post_type = 'runs'
                AND p.post_status = 'publish'
                AND m.meta_key = 'ssr_duration_min'
                AND t.slug IN $slugs
        ORDER BY    meta_value+0
        LIMIT       1

这基本上按价值排序,只接受第一个答案,即。最低。

于 2012-06-15T14:35:36.540 回答