MYSQL 查询组合两个表的数据共享第三个表的外键

发布于 2024-12-27 11:20:54 字数 297 浏览 2 评论 0原文

我正在尝试合并两个表 sales & 的数据。在一张桌子上购买。销售和销售从第三个表存储购买表共享外键。

构造查询以获取与结果表中完全相同的结果的最佳方法是什么,最好(如果可能)不使用子查询,因为我需要创建查询结果的视图,而 MYSQL 不允许创建视图使用子查询进行查询。

我尝试使用 union 选择,但没有得到预期的结果。

底层表和查询结果

I am trying to combine the data from two tables sales & purchase in one table. Both Sales & Purchase table shares foreign key from third table store.

What is the best way to construct the query to get the results exactly as in results tables, Preferably(if possible) without using sub-query, as I need to create a view of the query result and MYSQL doesn't allow create view on query using sub-query.

I have tried select with union but not getting the result as expected.

underlying tables & query result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌梦深 2025-01-03 11:20:54

以下查询(未经测试)应该给出您想要的结果集:

SELECT store_name AS store, MONTH(sales.date) AS month, SUM(sales.sales) AS sales, SUM(purchase.purchase) AS purchase
FROM store 
JOIN sales ON store.store_id = sales.store_id
JOIN purchase ON store.store_id = purchase.store_id
GROUP BY store.store_id,MONTH(sales.date), MONTH(purchase.date)

The following query(not tested) should give the resultset you want :

SELECT store_name AS store, MONTH(sales.date) AS month, SUM(sales.sales) AS sales, SUM(purchase.purchase) AS purchase
FROM store 
JOIN sales ON store.store_id = sales.store_id
JOIN purchase ON store.store_id = purchase.store_id
GROUP BY store.store_id,MONTH(sales.date), MONTH(purchase.date)
饭团 2025-01-03 11:20:54

我认为没有子查询就不可能获得预期的结果,因为您需要按月分组并从两个不同的表存储,每个表中可能有很多行,并在分组之前将它们连接在一起会增加行数。更重要的是,您需要对这些结果进行完全外连接,这在 mysql 中是不支持的,您必须通过两个相反的左连接的并集来模拟它。因此,基本上使用这个表模式,您必须使用一个非常复杂的查询,并且看起来更像是一个学术问题,而不是现实生活中的示例。

( SELECT st.store_name, s.month,
          COALESCE( s.salestotal, 0 ) as salestotal, 
          COALESCE( p.purchasetotal, 0 ) as purchasetotal
FROM 
  store st
  JOIN
    ( SELECT store_id, SUM(sales) as salestotal, 
             DATE_FORMAT( date, '%b %Y' ) as month
      FROM sales
      GROUP BY store_id, month ) s
  ON st.store_id = s.store_id
  LEFT JOIN
    ( SELECT store_id, SUM(purchase) as purchasetotal, 
      DATE_FORMAT( date, '%b %Y' ) as month
      FROM purchase
      GROUP BY store_id, month ) p
  ON p.store_id = s.store_id AND p.month = s.month )
UNION
( SELECT st.store_name, p.month,
          COALESCE( s.salestotal, 0 ) as salestotal, 
          COALESCE( p.purchasetotal, 0 ) as purchasetotal
FROM
  store st
JOIN
  ( SELECT store_id, SUM(purchase) as purchasetotal, 
           DATE_FORMAT( date, '%b %Y' ) as month
    FROM purchase
    GROUP BY store_id, month ) p
ON st.store_id = p.store_id
LEFT JOIN 
  ( SELECT store_id, SUM(sales) as salestotal, 
    DATE_FORMAT( date, '%b %Y' ) as month
    FROM sales
    GROUP BY store_id, month ) s
ON
  p.store_id = s.store_id AND p.month = s.month )
ORDER BY month, store_name

I don't think it is possible to get that expected result without subquery as you need to group by month and store from two different tables, both possibly having many rows in each group and joining them together before grouping would multiply rows. Even more, you need to do full outer join of these results which in mysql is not supported and you have to emulate it by union of two opposite left joins. So basically with this table schema you have to use a query that is quite complex and looks more like an academic problem than real life example.

( SELECT st.store_name, s.month,
          COALESCE( s.salestotal, 0 ) as salestotal, 
          COALESCE( p.purchasetotal, 0 ) as purchasetotal
FROM 
  store st
  JOIN
    ( SELECT store_id, SUM(sales) as salestotal, 
             DATE_FORMAT( date, '%b %Y' ) as month
      FROM sales
      GROUP BY store_id, month ) s
  ON st.store_id = s.store_id
  LEFT JOIN
    ( SELECT store_id, SUM(purchase) as purchasetotal, 
      DATE_FORMAT( date, '%b %Y' ) as month
      FROM purchase
      GROUP BY store_id, month ) p
  ON p.store_id = s.store_id AND p.month = s.month )
UNION
( SELECT st.store_name, p.month,
          COALESCE( s.salestotal, 0 ) as salestotal, 
          COALESCE( p.purchasetotal, 0 ) as purchasetotal
FROM
  store st
JOIN
  ( SELECT store_id, SUM(purchase) as purchasetotal, 
           DATE_FORMAT( date, '%b %Y' ) as month
    FROM purchase
    GROUP BY store_id, month ) p
ON st.store_id = p.store_id
LEFT JOIN 
  ( SELECT store_id, SUM(sales) as salestotal, 
    DATE_FORMAT( date, '%b %Y' ) as month
    FROM sales
    GROUP BY store_id, month ) s
ON
  p.store_id = s.store_id AND p.month = s.month )
ORDER BY month, store_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文