0

我有一个名为sales5 个字段的表:

  • id
  • date
  • id_store
  • id_customer
  • price

customers

  • customers.id
  • customers.name

和表stores

  • stores.id
  • stores.name

我想得到这个结果:

January, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
February, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
March, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
April...

可能吗?

4

1 回答 1

1

这是可能的,使用JOINsWITH ROLLUP. 基本上,您会将JOIN您的表放在一起以获取您需要的数据行,然后GROUP BY是多列(在您的情况下,可能GROUP BY MONTH(sales.date), stores.id, customers.id WITH ROLLUP)。按多列分组为您提供嵌套组,按列列出的顺序。WITH ROLLUP将为您提供每个嵌套级别的摘要数据;所以在我的例子中,你会得到每个客户每个月每个商店的总数,每个商店每个月的总数,以及每个月的总数。

于 2013-05-06T16:33:35.967 回答