如何返回MySQL中的枢轴表输出?
如果我有一张MySQL桌子看起来像这样:
Company_name | Action | PageCount |
---|---|---|
Company A | 3 | |
Company A | 2 | |
Company A | 3 | |
Company B | ||
B Print 2 Company B | 2 | |
Company B | 2 | |
Company B Print 2 Company B | 1 | |
Company 1 Company A | 3 |
可以运行一个mysql查询要以这样的输出:
company_name | 电子邮件 | 打印1页 | 打印2页 | 打印3页 |
---|---|---|---|---|
companya | 0 | 0 | 1 | 3 |
companyb | 1 | 1 | 2 | 0 |
想法是 pagecount
可以变化,以便输出列数量应反映这一点,每个操作
/ pagecount
配对的一列,然后每 company_name 。我不确定这是否称为枢轴表,但有人建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这基本上是一个枢轴表。
关于如何实现此目的的一个不错的教程可以在此处找到: http:http:// www。 Artfulsoftware.com/infotree/qrytip.php?id=78
我建议阅读此帖子并根据您的需求调整此解决方案。
Update
在当前不再可用之后,我觉得不得不为所有搜索MySQL Pivot答案提供一些其他信息。它确实有大量信息,我不会把所有内容都放在这里(甚至更多,因为我只是不想复制他们的知识),但是我会为如何处理枢轴提供一些建议PEKU的示例通常是SQL的表格,他首先问了这个问题。
也许链接很快就会回来,我会留意它。
电子表格方式...
许多人仅将MSEXCEL,OpenOffice或其他电子表格工具等工具用于此目的。这是一个有效的解决方案,只需将数据复制到那儿,然后使用GUI提供的工具来解决此问题。
但是...这不是一个问题,甚至可能导致一些缺点,例如如何将数据输入电子表格,有问题的缩放等等。
sql方法...
给定表情看起来像这样:
现在看他/她所需的桌子:
行(
email> emair>
,打印x页)类似于条件。主要分组是
company_name
。为了设置条件,这种情况却大喊使用
案例
- 陈述。为了通过某种东西进行分组,请使用... 组组。提供此枢轴的基本SQL可以看起来像这样:
这应该非常快速地提供所需的结果。这种方法的主要缺点,在枢轴表中想要的行越多,您需要在SQL语句中定义的条件越多。
这也可以处理,因此人们倾向于使用准备好的陈述,例程,柜台等。
有关此主题的一些其他链接:
This basically is a pivot table.
A nice tutorial on how to achieve this can be found here: http://www.artfulsoftware.com/infotree/qrytip.php?id=78
I advise reading this post and adapt this solution to your needs.
Update
After the link above is currently not available any longer I feel obliged to provide some additional information for all of you searching for mysql pivot answers in here. It really had a vast amount of information, and I won't put everything from there in here (even more since I just don't want to copy their vast knowledge), but I'll give some advice on how to deal with pivot tables the sql way generally with the example from peku who asked the question in the first place.
Maybe the link comes back soon, I'll keep an eye out for it.
The spreadsheet way...
Many people just use a tool like MSExcel, OpenOffice or other spreadsheet-tools for this purpose. This is a valid solution, just copy the data over there and use the tools the GUI offer to solve this.
But... this wasn't the question, and it might even lead to some disadvantages, like how to get the data into the spreadsheet, problematic scaling and so on.
The SQL way...
Given his table looks something like this:
Now look into his/her desired table:
The rows (
EMAIL
,PRINT x pages
) resemble conditions. The main grouping is bycompany_name
.In order to set up the conditions this rather shouts for using the
CASE
-statement. In order to group by something, well, use ...GROUP BY
.The basic SQL providing this pivot can look something like this:
This should provide the desired result very fast. The major downside for this approach, the more rows you want in your pivot table, the more conditions you need to define in your SQL statement.
This can be dealt with, too, therefore people tend to use prepared statements, routines, counters and such.
Some additional links about this topic:
我的解决方案是在没有任何枢轴的T-SQL中:
My solution is in T-SQL without any pivots:
对于MySQL,您可以将条件直接放入
sum()
函数中,并且将被评估为布尔值0
或 1 1 因此,您可以根据自己的条件进行计数,而无需使用If/case
语句demo
For MySQL you can directly put conditions in
SUM()
function and it will be evaluated as Boolean0
or1
and thus you can have your count based on your criteria without usingIF/CASE
statementsDEMO
对于动态枢轴,请使用
group_concat
与concat
。For dynamic pivot, use
GROUP_CONCAT
withCONCAT
.The GROUP_CONCAT function concatenates strings from a group into one string with various options.
DEMO HERE
A stardard-sql 使用:
db<> fiddle
old sqlfiddle
如何?
true或null
产生true
。false或null
产生null
。null或null
产生null
。and
cont a>仅计算非零值。瞧。
A stardard-SQL version using boolean logic:
db<>fiddle here
Old sqlfiddle
How?
TRUE OR NULL
yieldsTRUE
.FALSE OR NULL
yieldsNULL
.NULL OR NULL
yieldsNULL
.And
COUNT
only counts non-null values. Voilá.正确答案是:
Correct answer is:
有一个称为MySQL Pivot Table Generator的工具,它可以帮助您创建一个基于Web的枢轴表,稍后您可以将其导出到Excel(如果愿意)。如果您的数据位于单个表格或几个表中,则可以工作。
您需要做的就是指定列的数据源(它支持动态列),行,表主体中的值以及表关系(如果有)

此工具的主页是 https://mysqlreports.com/mysql-reporting-tools/mysql-pivot-table/
There is a tool called MySQL Pivot table generator, it can help you create a web-based pivot table that you can later export to excel(if you like). it can work if your data is in a single table or in several tables.
All you need to do is to specify the data source of the columns (it supports dynamic columns), rows, the values in the body of the table, and table relationship (if there are any)

The home page of this tool is https://mysqlreports.com/mysql-reporting-tools/mysql-pivot-table/
一个选项是组合
案例的使用。与 一起动态以及以下代码块中的结果集获得正确的列标题
:
Demo
One option would be combining use of
CASE..WHEN
statement is redundant within an aggregation for MySQL Database, and considering the needed query generation dynamically along with getting proper column title for the result set as in the following code block :Demo