gpt4 book ai didi

php - MYSQL 查询同一字段两次

转载 作者:行者123 更新时间:2023-11-29 14:28:41 24 4
gpt4 key购买 nike

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

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)。

有人能指出我正确的方向吗?

最佳答案

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

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

关于php - MYSQL 查询同一字段两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10437778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com