Jump to content
php.lv forumi

Sasummēt query


reiniger

Recommended Posts

Sveiki.

 

man ir diezgan sarežģītā situācijā jādabūt kopsumma. Man ir trīs informācijas tabulas un katrai tabulai ir papildus tabula ar preces skaitu.

 

Nepieciešams lai atlasot varu iegūt kopējo skaitu.

 

Mans query:

SELECT
d.net-COALESCE(SUM(t22.net),0)-COALESCE(adm.net,0)
FROM tabula_1_2 AS d
LEFT JOIN tabula_1 AS t1 ON (t1.id=d.id_p)
LEFT JOIN tabula_2_2 AS t22 ON (t22.id_p=d.id)
LEFT JOIN (
  SELECT t32.id_p, SUM(t32.net) AS Net FROM tabula_3_2 AS t32 GROUP by t32.id_p ) AS adm ON (adm.id_p=d.id)
WHERE d.id_p = 1
GROUP by d.id

Šīs variants man strādā, tikai visas vērtības NET es saglabāju masīvā un tad ar array_sum iegūstu kopsummu. Bet tas ir lieks darbs serverim un zinu ko ta visu var arī izdarīt datu bāzu pusē ar query.

 

Uz kopējās atņemšanas darbības SUM() un COUNT() man saka ka kļūda:

#1111 - Invalid use of group function

PS: šis query jau kādu laiku šajā forumā arī tika izveidots :)

 

Iepriekšējais veidojums

Edited by reiniger
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...