0

我有以下不返回结果的查询:

1. SELECT *
2. FROM wp_postmeta pm
3. LEFT JOIN wp_posts p ON (p.ID = pm.post_id)
4. LEFT JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
5. LEFT JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
6. WHERE p.post_status = 'publish' 
7. AND p.post_type = 'post' 
8. AND p.post_date < NOW()+INTERVAL 1 DAY 
9. AND tt.taxonomy = 'category' 
10. AND tt.term_id IN(3)
11. AND (pm.meta_key = 'EndDate' AND pm.meta_value > DATE_ADD(NOW(), INTERVAL 3 HOUR)) 
12. AND (pm.meta_key = 'NumPrizes' AND pm.meta_value > 1)
13. GROUP BY ID
14. ORDER BY p.ID DESC

问题是由于第 11 行和第 12 行而出现的。我可以删除其中一个并获得结果,但我需要两者才能获得我想要的。

我想要的是获得 pm.meta_key EndDate 大于今天且 pm.meta_key NumPrizes 大于 0 的结果。

这个想法是输出数据库中的奖品总数。

数据库是wordpress建的,不能改。它的工作方式是 meta_key 有一个值,它是字段的名称,而 meta_value 是 meta_key 的实际值。

wp_postmeta 表的示例:

meta_id     post_id    meta_key   meta_value
1           45         EndDate    2012-01-01    <- too old
2           45         NumPrizes  5             <- since too old, don't count
3           76         EndDate    2012-07-03    <- valid date but...
4           76         NumPrizes  1             <- only 1 prize so don't count
5           90         EndDate    2012-06-02    <- valid date and
6           90         NumPrizes  4             <- more than one prize so count it
7           192        EndDate    2012-09-01    <- valid date and
8           192        NumPrizes  9             <- more than one prize so count it

预期输出:奖品数量为 13。(4+9 = 13)。

谁能指出我正确的方向?

4

1 回答 1

1

当然,单个列不可能同时采用两个不同的值:

11. AND pm.meta_key = 'EndDate'      <<-- big error: column can't take two
12. AND pm.meta_key = 'NumPrizes'    <<--      differents values at a time

您应该规范化表和/或编写正确的查询:

1. SELECT *
2. FROM wp_postmeta pm
3. LEFT JOIN wp_posts p ON (p.ID = pm.post_id)
4. LEFT JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
5. LEFT JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
6. WHERE p.post_status = 'publish' 
7. AND p.post_type = 'post' 
8. AND p.post_date < NOW()+INTERVAL 1 DAY 
9. AND tt.taxonomy = 'category' 
10. AND tt.term_id IN(3)
11. AND (pm.meta_key = 'EndDate' AND pm.meta_value > DATE_ADD(NOW(), INTERVAL 3 HOUR)) 
12. AND exists (
          select 1 from wp_postmeta pm2 where
          pm2.post_id  = pm.post_id and
          pm2.meta_key = 'NumPrizes' AND pm2.meta_value > 1
    )
13. GROUP BY ID
14. ORDER BY p.ID DESC

已编辑

乔纳森,我不知道您的预期结果,因为您谈论的是单个标量结果(编号 13),但在您的查询中,您将主表与其他表连接起来。我希望我在这里编写的这段代码可以对您有所帮助,但您应该根据您的具体情况对其进行调整。

1. SELECT *, sum( __your_prizes_agrregation__ )
2. FROM wp_postmeta pm
3. LEFT JOIN wp_posts p ON (p.ID = pm.post_id)
4. LEFT JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
5. LEFT JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
6. WHERE p.post_status = 'publish' 
7. AND p.post_type = 'post' 
8. AND p.post_date < NOW()+INTERVAL 1 DAY 
9. AND tt.taxonomy = 'category' 
10. AND tt.term_id IN(3)
11. AND exists (
          select 1 from wp_postmeta pm2 where
          pm2.post_id  = pm.post_id and
          pm2.meta_key = 'EndDate' AND 
          pm2.meta_value > DATE_ADD(NOW(), INTERVAL 3 HOUR)
    )
12. AND exists (
          select 1 from wp_postmeta pm2 where
          pm2.post_id  = pm.post_id and
          pm2.meta_key = 'NumPrizes' AND pm2.meta_value > 1
    )
13. GROUP BY ID
14. ORDER BY p.ID DESC
于 2012-05-03T19:13:03.447 回答