gpt4 book ai didi

php - MYSQL计算避免重复值

转载 作者:行者123 更新时间:2023-11-29 04:46:54 26 4
gpt4 key购买 nike

我的 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

这是实际计算的两倍

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

请给我一些提示

最佳答案

尝试

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

输出:

| HTNO | TECH | DIVI | CRED | LOG | PASS | ATM | TOT |------------------------------------------------------|   12 |  153 | 76.5 |    2 |   0 |    2 |   2 |   0 |

这是 SQLFiddle 演示

关于php - MYSQL计算避免重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com