Mysql统计多个表中按天分组的记录
我的数据库有新闻文章和博客文章。两者的主键都是在两个表中唯一的 ItemID。
的表中
- 文章位于具有以下字段item_id
- title
- body
- date_posted
blogposts 表具有以下字段
- item_id
- title
- body
- date_posted
两个表都具有各自特有的额外字段。
我有第三个表,其中保存有关文章和帖子的元信息。
items 表具有以下字段
- item_id
- source_id
- ...
每篇博文和文章在 items 表中都有一条记录,并在其各自的表中都有一条记录。
我想做的是构建一个查询来计算每天发布的项目数。我可以使用按 date_posted 分组的计数对一个表执行此操作,但如何在一个查询中合并文章和帖子计数?
My database has news articles and blog posts. The primary key for both is an ItemID that is unique across both tables.
The articles are in a table that has the following fields
- item_id
- title
- body
- date_posted
The blogposts table has the following fields
- item_id
- title
- body
- date_posted
both tables have extra fields unique to them.
I have a third table that holds meta information about articles and posts.
The items table has the following fields
- item_id
- source_id
- ...
every blogpost and article has a record in the items table and a record in its respective table.
What I am trying to do is build a query that will count the number of items posted per day. I can do it for one table using a count grouped by date_posted but how to combine articles and posts count in one query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与民主党类似,但稍微简单一些:
Similar to Dems, but slightly simpler:
您可以通过两种方式做到这一点。
1. 将所有内容连接在一起,然后聚合(参见 Tom H 的回答)。
2. 聚合每个表,对它们进行 UNION,然后再次聚合。
选项 1 可能看起来更短,但意味着您可能无法从根表上的索引中受益(因为必须为 JOIN 重新排序它们)。所以我将展示选项 2,这是您无论如何都要去的方向。
这应该是最快的,前提是您在每个表上都有一个索引,其中
date_posted
是索引中的first 字段。否则,表仍需要重新排序以进行聚合。You can do it two ways.
1. Join everything together and then aggregate (See Tom H's answer).
2. Aggregate each table, UNION them, and aggregate again.
Option 1 may seem shorter, but will mean that you may not benefit from INDEXes on the root tables (As they have to be re-ordered for the JOIN). So I'll show option 2, which is the direction you were headed any way.
This should be fastest, provided that you have an index on each table where
date_posted
is the first field in the index. Other-wise the tables will still need to be re-ordered for the aggregation.我会为此使用不同的表设计,包括类型和子类型。您的 Items 表具有单列主键,Blog_Posts 和 Articles 表的主键与 Items 表的外键具有相同的 ID。这将使类似的事情变得非常容易,并且还有助于确保数据完整性。
根据您现有的设计,您最好的选择可能是这样的:
您还可以尝试使用
UNION
:I would have used a different table design for this, with types and subtypes. Your Items table has a single column primary key and your Blog_Posts and Articles tables' primary keys are the same ID with a foreign key to the Items table. That would make something like this pretty easy to do and also helps to ensure data integrity.
With your existing design, your best bet is probably something like this:
You could also try using a
UNION
:您可能需要将其放入子查询中,如果您愿意,可以在运行
group by
时将其与其他表连接起来。但要点是,union
是用于组合来自不同表的类似数据的运算符。union all
告诉数据库您不需要它来合并重复记录,因为您知道两个表永远不会共享 item_id,因此它会更快一些(可能)。You'll probably need to put that into a subquery, and if you so desire, join it with other tables, when running the
group by
. But the main point is thatunion
is the operator you use to combine like data from different tables.union all
tells the database that you don't need it to combine duplicate records, since you know that the two tables will never share an item_id, so it's a little faster (probably).