如何在html中以特定格式显示sql数据 [英] How to display sql data in a specific format in html

查看:260
本文介绍了如何在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:

  1. 选择感兴趣的列,即y值和x值
  2. 用额外的列扩展基本表-每个x值一个
  3. 分组并汇总扩展表-每个y值一组
  4. (可选)整理汇总表

这里是全文: MySQL-行到列

希望它会有所帮助.

关于

这篇关于如何在html中以特定格式显示sql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆