如何在 MYSQL 中使用 JOIN 和/或 UNION 计算不同表中的值?
我想将这些表中的 product_name
一起计数:
jan_product feb_product
+------------+ +------------+
|product_name| |product_name|
+------------+ +------------+
|A | |A |
+------------+ +------------+
|A | |B |
+------------+ +------------+
|C | |C |
+------------+ +------------+
我希望我的结果看起来像:
+------------+---------+---------+
|product_name|jan_count|feb_count|
+------------+---------+---------+
|A |2 |1 |
+------------+---------+---------+
|B |0 |1 |
+------------+---------+---------+
|C |1 |1 |
+------------+---------+---------+
所以我尝试了下面的查询(我使用的是 MYSQL,所以我无法尝试 FULL JOIN):
SELECT
j.product_name,
count(j.product_name) as jan_count,
count(f.product_name) as feb_count
FROM jan_product as j
JOIN feb_product as f
ON j.product_name = f.product_name
group by j.product_name
UNION
SELECT
f.product_name,
count(j.product_name) as jan_count,
count(f.product_name) as feb_count
FROM jan_product as j
RIGHT OUTER JOIN feb_product as f
ON f.product_name = j.product_name
group by f.product_name
;
但我得到了这个相反:
+------------+---------+---------+
|product_name|jan_count|feb_count|
+------------+---------+---------+
|A |2 |2 | --- A counts for FEB is wrong
+------------+---------+---------+
|B |0 |1 |
+------------+---------+---------+
|C |1 |1 |
+------------+---------+---------+
我不知道该怎么做才能达到预期的结果。
I want to count the product_name
in these tables together:
jan_product feb_product
+------------+ +------------+
|product_name| |product_name|
+------------+ +------------+
|A | |A |
+------------+ +------------+
|A | |B |
+------------+ +------------+
|C | |C |
+------------+ +------------+
I want my result to look like:
+------------+---------+---------+
|product_name|jan_count|feb_count|
+------------+---------+---------+
|A |2 |1 |
+------------+---------+---------+
|B |0 |1 |
+------------+---------+---------+
|C |1 |1 |
+------------+---------+---------+
So I tried the query below (I'm using MYSQL so I couldnt try FULL JOIN):
SELECT
j.product_name,
count(j.product_name) as jan_count,
count(f.product_name) as feb_count
FROM jan_product as j
JOIN feb_product as f
ON j.product_name = f.product_name
group by j.product_name
UNION
SELECT
f.product_name,
count(j.product_name) as jan_count,
count(f.product_name) as feb_count
FROM jan_product as j
RIGHT OUTER JOIN feb_product as f
ON f.product_name = j.product_name
group by f.product_name
;
But i got this instead:
+------------+---------+---------+
|product_name|jan_count|feb_count|
+------------+---------+---------+
|A |2 |2 | --- A counts for FEB is wrong
+------------+---------+---------+
|B |0 |1 |
+------------+---------+---------+
|C |1 |1 |
+------------+---------+---------+
I do not know what to do to get to the expected result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们可以通过 Union all 的计数来做到这一点。我宁愿建议您使用一张带有月份列的表格。
db<>fiddle 此处
We can do this with count from Union all. I would rather advise you to have one table with a month column.
db<>fiddle here
暂时忽略表结构...尝试使用 UNION ALL,包括一个额外的列来指示源月份。然后使用条件 SUM 计算每个月的总计。
另请参见 db<>fiddle
结果:
话虽如此,你应该标准化模型。您可以通过将所有内容存储在一个带有日期(或月+年列)的表中,而不是为每个月使用单独的表,从而大大简化事情。
此外,您似乎正在存储有关特定产品随时间发生的事件的信息。如果是这种情况,您应该有一个包含唯一产品的单独表:
存储有关产品信息的其他表应存储“Product”表的唯一 PK (主键)值 - 不是产品名称。例如,如果您有一个 ProductSales 表
要按月检索有关销售的信息,您所需要的只是两个表之间的简单 JOIN
另请参阅 db>>fiddle
结果:
Ignoring the table structure for a minute ... try using a UNION ALL, including an extra column to indicate the source month. Then use a conditional SUM to calculate the totals for each month.
See also db<>fiddle
Results:
Having said that, you should normalize the model. You could greatly simplify things by storing everything in a single table, with a date (or month + year columns) instead of having a separate table for each month.
Also, it seems like you're storing information about events that occur to a specific product over time. If that's the case, you should have a separate table containing unique products:
Other tables that store information about products should store the "Product" table's unique PK (primary key) value - not a product's name. For example, if you had a ProductSales table
To retrieve information about the sales by month, all you'd need is a simple JOIN between the two tables
See also db<>fiddle
Results: