gpt4 book ai didi

MySQL 连接查询计数为每个结果返回一行

转载 作者:行者123 更新时间:2023-11-29 14:22:33 25 4
gpt4 key购买 nike

因此,我正在执行自定义 WordPress 查询来提取事件(事件存储在自定义字段(元数据)中,因此需要 JOIN),并且我想获取返回的行数。我的问题是,我收到了一堆返回的行,每行都包含针对返回的每个帖子 ID 的计数。

我的查询看起来有点像这样:

SELECT count(*)
FROM wp_posts AS wposts
LEFT JOIN wp_postmeta AS metafeatured
ON wposts.id = metafeatured.post_id
LEFT JOIN wp_postmeta AS metastart
ON wposts.id = metastart.post_id
LEFT JOIN wp_postmeta AS metaend
ON wposts.id = metaend.post_id
LEFT JOIN wp_term_relationships
ON ( wposts.id = wp_term_relationships.object_id )
LEFT JOIN wp_term_taxonomy
ON ( wp_term_relationships.term_taxonomy_id =
wp_term_taxonomy.term_taxonomy_id )
WHERE wposts.post_type = 'll_events'
AND wposts.post_status = 'publish'
AND ( ( ( metastart.meta_key = 'll_events_startdate'
AND Cast(metastart.meta_value AS UNSIGNED) < 1342997940 )
AND ( metaend.meta_key = 'll_events_enddate'
AND Cast(metaend.meta_value AS UNSIGNED) > 1342525664 ) )
OR ( ( metastart.meta_key = 'll_events_startdate'
AND Cast(metastart.meta_value AS UNSIGNED) <= 1342997940 )
AND ( metaend.meta_key = 'll_events_enddate'
AND Cast(metaend.meta_value AS UNSIGNED) >= 1342525664
) )
OR ( ( metastart.meta_key = 'll_events_startdate'
AND Cast(metastart.meta_value AS UNSIGNED) BETWEEN
1342525664 AND 1342997940
)
AND ( metastart.meta_key = 'll_events_enddate'
AND Cast(metaend.meta_value AS UNSIGNED) BETWEEN
1342525664 AND 1342997940
) )
OR ( ( metastart.meta_key = 'll_events_startdate'
AND Cast(metastart.meta_value AS UNSIGNED) BETWEEN
1342525664 AND 1342997940
)
AND ( metaend.meta_key = 'll_events_enddate'
AND Cast(metaend.meta_value AS UNSIGNED) <= 1 ) ) )
GROUP BY wposts.ID, metastart.post_id, metaend.post_id, wp_term_relationships.object_id
ORDER BY metastart.meta_value ASC

我已经尝试了组、计数等的大量组合,但无法获得预期结果(结果集中的一行包含“计数”)。相反,我的结果集看起来像:

count(*)
24
30
30
30
12
16
1

...等等,我需要的地方:

count(*)
139

最佳答案

只需删除 GROUP BY 子句

关于MySQL 连接查询计数为每个结果返回一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11523193/

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