1

我的 MySQL 表是

 htno     name       fathername       subcode      subject       internals     externals     total    result      tempcr
 12      Mahesh        Rajesh           256         Maths           20            55           75        P           1
 12      Mahesh        Rajesh           257        Science          18            60           78        P           1
 12      Mahesh        Rajesh           256         Maths           20            55           75        P           1
 12      Mahesh        Rajesh           257        Science          18            60           78        P           1

表有重复行

我需要的输出是

HTNO     TOTAL MARKS SECURED       PERCENTAGE       TOTAL SUBJECTS       SUBJECTS ATTEMPTED      SUBJECTS ABSENT         SUBJECTS PASSED        BACKLOGS
12               153                   76.5%             2                      2                       0                    2                       0

我得到的输出是

HTNO     TOTAL MARKS SECURED       PERCENTAGE       TOTAL SUBJECTS       SUBJECTS ATTEMPTED      SUBJECTS ABSENT         SUBJECTS PASSED        BACKLOGS
12               306                   153%              4                      4                       0                    4                       0

这是实际计算的两倍

我想在此计算中排除那些重复的行

请给我一些提示

4

2 回答 2

4

尝试

SELECT htno, 
       SUM(total)               tech, 
       ROUND(SUM(total) / 2, 2) divi, 
       SUM(tempcr)              cred, 
       SUM(tempcr <= 0)         log,
       SUM(tempcr >  0)         pass, 
       SUM(externals >= 0)      atm, 
       SUM(externals <  0)      tot
  FROM 
( 
  SELECT DISTINCT * 
    FROM Table1 
   WHERE htno = 12
) q

输出:

| 亚硝酸盐 | 技术 | 迪维 | 信用 | 日志 | 通过 | 自动柜员机 | 托特 |
-------------------------------------------------- ----
| 12 | 153 | 76.5 | 2 | 0 | 2 | 2 | 0 |

这是SQLFiddle演示

于 2013-07-19T08:05:28.040 回答
0

使用 GROUP_BY 或 DISTINCT:

$sum = $db->prepare("
    SELECT htno, 
    SUM(total) AS tech, 
    ROUND(SUM(total)/7.5, 2) AS divi, 
    SUM(tempcr) AS cred, 
    SUM(CASE WHEN tempcr <= 0 THEN 1 ELSE 0 END) AS log,
    SUM(CASE WHEN tempcr > 0 THEN 1 ELSE 0 END) AS pass, 
    SUM(CASE WHEN externals >= 0 THEN 1 ELSE 0 END) AS atm, 
    SUM(CASE WHEN externals >= -2 THEN 1 ELSE 0 END) AS tot 
    FROM btech32r09july2013 
    WHERE htno = :id 
    GROUP_BY subject"
);
于 2013-07-19T07:49:19.213 回答