我需要2个和数之和的帮助。 好吧,我有一个表显示用户总数的表。 在表格末尾,我需要制作显示所有数字总和的行。
表:
所以在蓝线上我需要所有数字的总和。 我的代码:
$groupQuery =
"SELECT r.broj_rac AS racun, COUNT(stavka) AS stavka, d.naziv1 AS dobavljac, SUM(iznos_osnovica) AS osnovica, SUM(iznos_porez) AS porez, SUM(moze_se_odbiti) AS odbiti, SUM(nemoze_se_odbiti) AS neodbiti, SUM(neoporezivo) AS neoporezivo, SUM(ukupno) AS ukupno1, r.sveukupno AS sveukupno
FROM racuni AS r
LEFT JOIN dobavljaci AS d
ON d.sifra = r.dobavljac
WHERE partner = '".$_SESSION['sifra_partnera']."'
GROUP by broj_rac";
是否可以制作类似SUM(SUM(ukupno AS ukupno1) AS ukupno2)
的内容?
答案 0 :(得分:2)
看起来你想要添加一个总行,而不是像其他答案所建议的那样添加总和列,所以如果你只想包含一个总计osnovica
和{{1}的行,那么这样的话应该有效}值:
porez
在$groupQuery =
SELECT r.broj_rac AS racun,
COUNT(stavka) AS stavka,
d.naziv1 AS dobavljac,
SUM(iznos_osnovica) AS osnovica,
SUM(iznos_porez) AS porez,
SUM(moze_se_odbiti) AS odbiti,
SUM(nemoze_se_odbiti) AS neodbiti,
SUM(neoporezivo) AS neoporezivo,
SUM(ukupno) AS ukupno1,
r.sveukupno AS sveukupno
FROM racuni AS r
LEFT JOIN dobavljaci AS d
ON d.sifra = r.dobavljac
WHERE partner = '".$_SESSION['sifra_partnera']."'
GROUP by broj_rac
UNION
SELECT 'TOTAL' AS racun,
'' AS stavka,
'' AS dobavljac,
SUM(iznos_osnovica) AS osnovica,
SUM(iznos_porez) AS porez,
'' AS odbiti,
'' AS neodbiti,
'' AS neoporezivo,
'' AS ukupno1,
'' AS sveukupno
FROM racuni AS r
LEFT JOIN dobavljaci AS d
ON d.sifra = r.dobavljac
WHERE partner = '".$_SESSION['sifra_partnera']."'"
的后半部分,您还可以在您希望在总行中汇总的任何其他字段上执行汇总功能,例如UNION
或SUM(ukupno) AS ukupno1
AVG(ukupno) AS ukupno1
的总和或平均值。
答案 1 :(得分:1)
尝试以下查询
SELECT
(
SUM(COL_1)
+ SUM(COL_2)
+ SUM(COL_3)
) as `Total`
FROM TABLE_NAME
答案 2 :(得分:0)
你可以做到
SELECT
Val1,
Val2,
Val3,
(Val1 + Val2 + Val3) as 'Total'
FROM MyTable
或者如果你只想要一行:
SELECT
SUM(Val1) as 'Val1',
SUM(Val2) as 'Val2',
SUM(Val3) as 'Val3',
(SUM(Val1) + SUM(Val2) + SUM(Val3)) as 'Total'
FROM MyTable
答案 3 :(得分:0)
尝试以下查询
SELECT
(
SUM(COL_1)
+ SUM(COL_2)
+ SUM(COL_3)
) as `Total`
FROM TABLE_NAME