简体   繁体   English

在两个表上具有JOIN的MySQL COUNT

[英]mySQL COUNT with JOIN on two tables

first of all, sorry, i'm not good in mySQL right now. 首先,对不起,我现在的MySQL不好。

I've got 2 tables: 我有2张桌子:

post_meta

id | post_id | page_num
4  | 161     | 14
3  | 160     | 9
2  | 159     | 3
1  | 158     | 16

And: 和:

wp_posts

id  | date                 | parent_id
161 | 2013-08-28 13:53:48  | 160
160 | 2013-07-28 13:53:48  | 0
159 | 2013-04-28 13:53:48  | 158
158 | 2013-02-28 13:53:48  | 0

I want to know the sum of all page_nums for the posts where the date is 2013. The problem is that 1 single post got severals other post which got the parent_id from the parent post. 我想知道日期为2013的帖子的所有page_nums的总和。问题是1个单独的帖子得到了几个其他的帖子,这些帖子又从父帖子中获取了parent_id。

In this example the id 160 is the parent post in wp_posts and got a child post with the id 161. But it's the same single post. 在此示例中,id 160是wp_posts中的父帖子,并获得了ID为161的子帖子。但这是同一篇文章。

The page_num from post_meta got the post_id from each post even it's a child. 来自post_meta的page_num即使是孩子,也从每个帖子中获取了post_id。 In this case it would be id 4 and 2 from post_meta since that are the latest saves for the parent posts 160 and 159. 在这种情况下,它将是post_meta的ID 4和2,因为这是父帖子160和159的最新保存。

So i only need to sum up only the page_nums for the latest (newest) child post . 因此,我只需要总结最新(最新)子帖子的page_nums。

My (very bad) try so far: 到目前为止,我的尝试(非常糟糕):

COUNT pagenum FROM wp_postmeta a WHERE wp_postmeta b post_id.b = parent_id.a AND DATE LIKE='2014' GROUP BY parent_id.b

I hope you understand the problem. 希望你理解这个问题。

Thanks for your help in advance and 感谢您的提前帮助,

best regards 最好的祝福

if you only have one level of child/parent relationship you can use something like below to grab 1)parents without child and 2)latest child of each parent using group by 如果您只有一个子女/父母关系,则可以使用下面的方法来抓取1)没有子女的父母和2)使用分组依据的每个父母的最新子女

SELECT meta.*
FROM post_meta meta
INNER JOIN
(SELECT parent.id FROM wp_posts parent      -- parent without child
 WHERE YEAR(date) = 2013 
      AND parent_id = 0 
      AND NOT EXISTS (SELECT 1 FROM wp_posts child
                  WHERE child.parent_id = parent.id)
 UNION
 SELECT max(id) FROM wp_posts               -- latest child
 WHERE YEAR(date) = 2013
 AND parent_id > 0
 GROUP BY parent_id
 )posts
ON posts.id = meta.post_id

sqlFiddle sqlFiddle

Then you can sum page_num using SUM(meta.page_num) as whateverNameYouLike like in this sqlFiddle 然后,您可以使用SUM(meta.page_num) as whateverNameYouLikesqlFiddle中的whatNameLike之和来对page_num求和

Note: The above query is making the assumption that the id in wp_posts table will be largest for a lastest date, if this isn't the case you'll have to grab the max(date) , and id combination and look for latest entry that way. 注意:上面的查询假设wp_posts表中的id对于最新日期将是最大的,如果不是这种情况,则必须获取max(date)id组合并查找最新条目那样。 Which shouldn't be too complex. 哪个不应该太复杂。

I also added an entry 162 with no child to test for cases of posts that have no children. 我还添加了一个没有孩子的条目162,以测试没有孩子的帖子的情况。 That's why the sum of page_num is 18 instead of 17. 这就是page_num之和为18而不是17的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM