2014-09-12 74 views
1
SELECT z.`id`, z.`ukupno` ,s.uk, (ukupno-uk) as r from zbirni z 
join 
(SELECT k.`iddzid`, (sum(kolicina*cjena)) as uk FROM kasa k join kasa_detalji d on d.idd=k.id group by iddzid) as s 
on s.iddzid=z.id 
where (ukupno-uk)>0 

一個觀點,但我有錯誤 腳本行:1個視圖的SELECT語句包含在FROM子句中創建從SELECT語句的多個子查詢

回答

0

子查詢可以重寫查詢與聯接,以避免子查詢的使用和MySQL視圖的限制

SELECT 
z.`id`, 
z.`ukupno` , 
SUM(kolicina * cjena) uk, 
(ukupno - SUM(kolicina * cjena)) AS r 
FROM zbirni z 
JOIN kasa k ON(z.id=k.`iddzid`) 
JOIN kasa_detalji d ON (d.idd=k.id) 
GROUP BY z.`id` 
HAVING r > 0 

或每個子查詢創建視圖,並用它在你看來像

CREATE VIEW calc_sum 
AS 
SELECT k.`iddzid`, 
SUM(kolicina*cjena) AS uk FROM kasa k 
JOIN kasa_detalji d ON d.idd=k.id 
GROUP BY iddzid 

使用calc_sum在您的查詢中查看

CREATE VIEW your_view_name 
SELECT 
z.`id`, 
z.`ukupno` , 
s.uk, 
(ukupno-uk) AS r 
FROM zbirni z 
JOIN calc_sum AS s ON s.iddzid=z.id 
WHERE (ukupno-uk)>0