如何在html中以特定格式显示sql数据
问题描述:
我在Mysql中具有以下格式的数据:
I have data in Mysql in this format :
name sub
----------------
a maths
a science
a history
b maths
b science
a computer
a english
c computer
c history
b history
c maths
我打算以HTML格式显示此数据:
I am planning to display this data in this format in HTML:
Name maths science history computer english
a y y y y y
b y y y n n
c y n y y n
除数据透视表方法外,如何制定我的sql查询?
How to formulate my sql query other than pivot table method?
答
我在这里发现了类似的情况.让我简要介绍一下问题和解决方案:
I found a similar case here. Let me brief a little bit of the problem and solution:
问题:
将其转换为:
select * from history;
+--------+----------+-----------+
| hostid | itemname | itemvalue |
+--------+----------+-----------+
| 1 | A | 10 |
| 1 | B | 3 |
| 2 | A | 9 |
| 2 | C | 40 |
+--------+----------+-----------+
此内容:
select * from history_itemvalue_pivot;
+--------+------+------+------+
| hostid | A | B | C |
+--------+------+------+------+
| 1 | 10 | 3 | 0 |
| 2 | 9 | 0 | 40 |
+--------+------+------+------+
解决方案:
本文中,作者做了以下步骤:
From the article, here are the steps the author did:
- 选择感兴趣的列,即y值和x值
- 用额外的列扩展基本表-每个x值一个
- 分组并汇总扩展表-每个y值一组
- (可选)整理汇总表
这里是全文: MySQL-行到列
希望它会有所帮助.
关于