Jump to content
php.lv forumi

1 query + 2 SUM ar dažādiem WHERE


anonīms

Recommended Posts

tabula km1, km2, km1_rez, km2_rez

Nepieciešams iegūt top 5 komandām pēc iesistajiem.

Izvadu ar while top 5 komandas.

Ideja ir tāda, bet nezinu kā to dabūt gatavu jau iekšs kverija.

$Q = "(SELECT sum(km1_rez)FROM speles WHERE km1 = ?) AS goli1, (SELECT sum(km2_rez) AS goli1 FROM speles WHERE km2 = ?) AS goli2"; // order by goli1+goli2 DESC
while($qq = fetch_assoc($Q)
{

}

Vajag iekšs (?) - jautājumzīmēm dabūt ID. Ceru, ka ideja tapa skaidra.

Iepildīta tabula izsaktās apmēram šādi

km1, km2, km1_rez, km2_rez

1, 6, 14, 2

3, 1 5, 4

Link to comment
Share on other sites

es kko tādu uz ātro roku samurgoju :D

sintakse nav pareiza, bet ceru ka ideja ir skaidra

SELECT komanda, SUM(rez) as rezultats
FROM
 (SELECT SUM(km1_rez) as rez, km1 as komanda
 FROM SPELES
 GROUP BY km1
 UNION 
 SELECT SUM(km2_rez) as rez, km2 as komanda
 FROM SPELES
 GROUP BY km2) as sr
GROUP BY komanda
ORDER BY rez DESC

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...