2014-01-06 183 views
0

您好,我想通過查詢中的mcap命令我的查詢,但我收到錯誤,但我找不到合適的解。這裏的查詢:MySQL子查詢錯誤錯誤1242(21000):子查詢返回多於1行

SELECT 

c.code, 
(e.total_value - 
IFNULL((SELECT total_value FROM eod_stock WHERE company_id = e.company_id AND entry_date = 

SELECT 
entry_date FROM eod_stock WHERE entry_date < e.entry_date AND company_id = e.company_id ORDER 

BY entry_date DESC LIMIT 0,1)),0))/ 

(SELECT total_value FROM eod_stock WHERE company_id = e.company_id AND entry_date = (SELECT entry_date FROM eod_stock WHERE entry_date < e.entry_date AND company_id = e.company_id ORDER BY entry_date DESC LIMIT 0,1)) AS turnover_growth, 

(SELECT total_share FROM share_percentage WHERE company_code = c.code) * e.ltp AS mcap, 

'' AS pe 

-- ,e.* 

FROM eod_stock AS e 

LEFT OUTER JOIN company AS c 

ON c.ID = e.company_id 

WHERE e.company_id AND e.entry_date = (SELECT MAX(entry_date) FROM eod_stock) 

ORDER BY mcap DESC 

LIMIT 0,10; 

但它返回一個錯誤:

ERROR 1242 (21000): Subquery returns more than 1 row 

不知道哪裏出了問題。誰能幫我?

回答

1

不是一個全球性的聚集或有限制條款的唯一子查詢:

(SELECT total_share FROM share_percentage WHERE company_code = c.code) 

我猜想,這回不止一行。

編輯:

你如何解決它?這取決於邏輯。這裏有兩種看起來很明智的方法:

(SELECT sum(total_share) FROM share_percentage WHERE company_code = c.code) 

(SELECT total_share FROM share_percentage WHERE company_code = c.code limit 1) 
+0

我知道但是如何解決它? –

+0

是的第二個伎倆。謝謝。首先在邏輯上不正確cal total_share =! sum(total_share),即使它給MySQL錯誤也會產生錯誤的結果。 –

相關問題