duanjuelu8874 2016-11-10 07:22
浏览 694

在一个表TD中显示两行数据

Hello Please help me for the following problem

Mysql DATA table is

Sno Name    Subject Marks 
1   Test1   Sub1    20
2   Test1   Sub2    20
3   Test1   Sub3    20
4   Test1   Sub4    20
5   Test1   Sub5    20
6   Test2   Sub1    30
7   Test2   Sub2    30
8   Test2   Sub3    30
9   Test2   Sub4    30
10  Test2   Sub5    30
11  Test3   Sub1    40
12  Test3   Sub2    40
13  Test3   Sub3    40
14  Test3   Sub4    40
15  Test3   Sub5    40

i want to display like this

Sno Name    Marks
1   Test1   100
    Sub1    20
    Sub2    20
    Sub3    20
    Sub4    20
    Sub5    20
2   Test2   150
    Sub1    30
    Sub2    30
    Sub3    30
    Sub4    30
    Sub5    30
3   Test3   200
    Sub1    40
    Sub2    40
    Sub3    40
    Sub4    40
    Sub5    40

is it possaible in MYSQL PHP.

  • 写回答

1条回答 默认 最新

  • dpxo13079 2016-11-10 07:41
    关注

    Two ways of doing this

    1) Find all unique names and sum of marks

    Select Name  AS Heading, SUM(Marks) AS Total FROM table GROUP BY Name
    

    and then iterate through the results and query for all records matching the name

    Select * from table WHERE Name = $result['Heading']
    

    2) Get all records, then iterate through the results to group them

    Select * from table WHERE
    
    $headings = [];
    $children = [];
    foreach($allrecords as $result)
    {
        if (!in_array($result['Name'], $headings)
        {
             $headings[$result['Name']]['Name'] = $result['Name'];
        }
        if (isset($headings[$result['Name']]['Marks']))
        {
            $headings[$result['Name']]['Marks'] += $result['Marks']
        }
        else
        {
            $headings[$result['Name']]['Marks'] = $result['Marks'] 
        }
        $children[$result['Name']][] = $result;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图