doupu1957 2017-07-24 09:12
浏览 94
已采纳

PHP + Mysql显示一行中多行的值

Hello i have always found here the solution to my troubles but this time i really don't know how to search for what i need, then i will post here hope (but i'm sure) some one can help me make this query working.

I'm the developer of real rally (A richard Burns Rally Mod bla bla bla). We need now an automated system to display the final standing for each championship.

I have acquired all the necessary info from 16 querys and stored all them into a temp db.

this mean i have for each championship (Montecarlo, Sweden, Mexico and so on) 15 results inside the temp table. 15 for montecarlo 15 for sweden and so on

then i run this query

SELECT Id, totale, num_prove, IdPilota, Nome, Cognome, Team, Nazionalita, Plate, Classe, VetturaUsata, Penalita, PaPenalty, PuntiCarriera, PuntiCarrieraP,
PuntiMon, PuntiSve, PuntiMes, PuntiArg, PuntiPor, PuntiIta, PuntiPol, PuntiFin, PuntiGer, PuntiCin, PuntiFra, PuntiSpa, PuntiGrb, PuntiAus, RallyCorso,
SUM(PuntiMon) + SUM(PuntiSve) + SUM(PuntiMes) + SUM(PuntiArg) + SUM(PuntiPor) + SUM(PuntiIta) + SUM(PuntiPol) + SUM(PuntiFin) + SUM(PuntiGer) + SUM(PuntiCin) + SUM(PuntiFra) + SUM(PuntiSpa) + SUM(PuntiGrb) + SUM(PuntiAus) AS Punti
FROM temp_table_5975a9e73a835
GROUP BY IdPilota
ORDER BY Punti DESC;

now the whats wrong. This query work but i need to display for every IdPilota wich point he reach in every championship.

atm the displayed data show 43 0 0 0 0 0 due to group  by IdPilota:

but i need to disaplay the right point gained in every championship

There is a way to display what i need?

43 <- from (Montecarlo)
33 <- from (Sweden)
43 <- from (Mexico)
43 <- from (Argentina)
43 33 43 43

Hope to have explained what i need!!!

EDIT: This is the working query atm:

    SELECT Id, totale, num_prove, IdPilota, Nome, Cognome, Team, Nazionalita, Plate, Classe, VetturaUsata, Penalita, PaPenalty, PuntiCarriera, PuntiCarrieraP,
SUM(PuntiMon), SUM(PuntiSve), SUM(PuntiMes), SUM(PuntiArg), SUM(PuntiPor), SUM(PuntiIta), SUM(PuntiPol), SUM(PuntiFin), SUM(PuntiGer), SUM(PuntiCin), SUM(PuntiFra), SUM(PuntiSpa), SUM(PuntiGrb), SUM(PuntiAus), RallyCorso,
SUM(PuntiMon) + SUM(PuntiSve) + SUM(PuntiMes) + SUM(PuntiArg) + SUM(PuntiPor) + SUM(PuntiIta) + SUM(PuntiPol) + SUM(PuntiFin) + SUM(PuntiGer) + SUM(PuntiCin) + SUM(PuntiFra) + SUM(PuntiSpa) + SUM(PuntiGrb) + SUM(PuntiAus) AS Punti
FROM temp_table_5975a9e73a835
GROUP BY IdPilota
ORDER BY Punti DESC;

Just to know if this is a correct way to display the data i need

  • 写回答

1条回答 默认 最新

  • dtcuv8044 2017-07-24 14:13
    关注

    I do not know if you have specific restrictions or limitations but I do not usually mix business logic with presentation logic. In this case i would prefer don't create a temp table (maybe a view is better) because you use it in order to show a table on the presentation layer. I assume i have a data model like this (i repeat: i don't know if it is your case)

    TABLE "PILOTI"

    +--------+-------------+
    |  ID    | NOME        |
    +--------+-------------+
    |   1    | Colin McRea |
    +--------+-------------+
    |   2    | Carlos Sainz|
    +--------+-------------+
    

    TABLE "GARE"

    +--------+-------------+
    |  ID    | NOME        |
    +--------+-------------+
    |   1    | Montecarlo  |
    +--------+-------------+
    |   2    | Argentina   |
    +--------+-------------+
    |   3    | Svezia      |
    +--------+-------------+
    

    TABLE "CAMPIONATO" (n-n => relation table between GARE and PILOTI)

    +--------+-------------+-------------+-------------+
    |  ID    | ID_PILOTA   |  ID_GARA    | PUNTEGGIO   |
    +--------+-------------+-------------+-------------+
    |   1    |    1        |    1        |     43      |
    +--------+-------------+-------------+-------------+
    |   2    |    1        |    2        |     33      |
    +--------+-------------+-------------+-------------+
    |   3    |    2        |    1        |     15      |
    +--------+-------------+-------------+-------------+
    |   4    |    2        |    2        |     18      |
    +--------+-------------+-------------+-------------+
    

    With this query, i retrieve for each driver, the points reached every championship

    SELECT P.NOME, G.NOME, C.PUNTEGGIO
    FROM CAMPIONATO C
    JOIN PILOTI P ON P.ID = C.ID_PILOTA
    JOIN GARE   G ON G.ID = C.ID_GARA
    

    You can add a "WHERE" condition in order to filter for a single driver (or single championship). For example, if you have 15 drivers, you can retrieve the points reached to each driver (for each championship) with only 15 query adding

    WHERE P.ID = :id_pilota
    

    to the prevoious query.

    You may also create a view with this query and calculate the total points but i prefer to do this in PHP and not in MySQL. I.E. (PSEUDO-CODE)

    foreach($driver){
     queryToGetPoints();
     foreach($championship){
       $totalPoints+=$points;
     }
     printHtmlRow();
    }
    

    However, With MySql the ordering is more simply

    SELECT P.NOME,SUM(C.PUNTEGGIO) AS TOTALE
    FROM CLASSIFICA C
    JOIN  PILOTI P ON P.ID = C.ID_PILOTA
    GROUP BY C.ID_PILOTA 
    ORDER BY TOTALE ASC
    

    In order to update the points, after a championship is closed, you can insert in "CAMPIONATO" table, for each driver, a new row whit his own ID, the ID of the championship and the points reached.

    I.E.

    INSERT INTO CAMPIONATO (ID_PILOTA, ID_GARA, PUNTEGGIO) VALUES (1,3,18)
    

    => McRea reached 18 points in Svezia

    You may also create a table "CLASSIFICA" whit two fields: ID_PILOTA e TOTALE then create a trigger on "CAMPIONATO" table "after insert" in order to update the CAMPIONATO.TOTALE field with the sum of the old value + the points inserted.

    So, you can see there is more than one way in order to do what you want. My suggestion is: if one more query help you to get your code more simply, use it even if there is a computational overhead (multiple queries in nested loop in this case). Hope this help you. Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路