mysql sum s列为子查询结果的v

时间:2013-11-03 17:29:39

标签: mysql

我想将s列与此结果中的v相加

no_peserta nama_peserta      kejuruan        tulis   wawancara fisik  s       v
13001      FAHRI NUR AKBAR   MESIN INDUSTRI  78.75   80.0      80.0   51.354  ?
13002      ANGGA SAMSUROTO   MESIN INDUSTRI  78.75   80.0      80.0   51.354  ?
13003      AFIP LEO PERNANDI MESIN INDUSTRI  78.75   80.0      80.0   51.354  ?

这是我的查询

SELECT p.no_peserta, 
       p.nama_peserta, 
       tulis.rata2, 
       wawancara.rata2, 
       fisik.rata2, 
       ( 
       TRUNCATE(POW(tulis.rata2, 0.3), 3) ) * ( 
       TRUNCATE(POW(wawancara.rata2, 0.3), 3) ) * ( 
       TRUNCATE(POW(fisik.rata2, 0.3), 3) 
       ) AS s 
FROM   peserta p 
       INNER JOIN test_tulis AS tulis 
               ON p.idpeserta = tulis.idpeserta 
       INNER JOIN test_wawancara AS wawancara 
               ON p.idpeserta = wawancara.idpeserta 
       INNER JOIN test_fisik AS fisik 
               ON p.idpeserta = fisik.idpeserta 
LIMIT  3 

感谢并感谢任何回答...

2 个答案:

答案 0 :(得分:0)

如果您只需要总和结果,请尝试

select sum(r.s) as v 
from
(select p.no_peserta, p.nama_peserta, tulis.rata2, wawancara.rata2, fisik.rata2,
(truncate(pow(tulis.rata2,0.3),3))*(truncate(pow(wawancara.rata2,0.3),3))*
(truncate(pow(fisik.rata2,0.3),3)) as s
from peserta as p
inner join test_tulis as tulis on p.idpeserta=tulis.idpeserta
inner join test_wawancara as wawancara on p.idpeserta=wawancara.idpeserta
inner join test_fisik as fisik on p.idpeserta=fisik.idpeserta
limit 3) as r

答案 1 :(得分:0)

最后2天后我发现它来自Combining two queries into one 所以查询如下:

create temporary table tmp
select sum(ts.s) as sm
from 
(select
truncate(pow(t.rata2,0.3),3)*truncate(pow(w.rata2,0.3),3)*truncate(pow(f.rata2,0.3),3) as s
from test_tulis t,test_wawancara w, test_fisik f where t.idpeserta=w.idpeserta and
t.idpeserta=f.idpeserta limit 3
) ts

create temporary table tmp2
select a1,a2,x,y,z,a*b*c as s
from
(select truncate(pow(test_tulis.rata2,0.3),3) as a,
truncate(pow(test_wawancara.rata2,0.3),3) as b,
truncate(pow(test_fisik.rata2,0.3),3) as c,
test_tulis.rata2 as x,test_wawancara.rata2 as y, test_fisik.rata2 as z,
peserta.no_peserta as a1, peserta.nama_peserta as a2
from test_tulis,test_wawancara,test_fisik,peserta where peserta.kejuruan='mesin industri'
and test_tulis.idpeserta=peserta.idpeserta and test_wawancara.idpeserta=peserta.idpeserta
and test_fisik.idpeserta=peserta.idpeserta
group by a1
) as t

创建临时表之后:

select tmp2.*,tmp.sm,tmp2.s/tmp.sm as v
from tmp,tmp2

结果:

a1    a2                 x     y    z    s      v       s/v
13001 FAHRI NUR AKBAR    78.75 80.0 80.0 51.354 154.062 0.3333333
13002 ANGGA SAMSUROTO    78.75 80.0 80.0 51.354 154.062 0.3333333
13003 AFIP LEO PERNANDI  78.75 80.0 80.0 51.354 154.062 0.3333333

顺便感谢所有参与/回答我的问题。