2014-03-26 182 views
0

我在MySQL過程中有錯誤,因爲我試圖在'Where'參數中選擇多行。當我把'Where = param_oid'(檔案錶行的oid)放到整個過程中,但我想從檔案中返回幾行(所以不是調用表courrier_concerne_dossier中存在的檔案中的每個oid的過程)無論如何 - 我是試圖找到解決方案。子查詢在MySQL中返回多於1行where子句

有沒有簡單的方法來解決這個問題?

DROP procedure if exists `courrier_envoye_pickdossiers`; 

DELIMITER $$ 
CREATE procedure `courrier_envoye_pickdossiers`(IN param_oid binary(16)) 
BEGIN 

    set param_oid = (select oid from courrier_envoye where numero_chrono_ordre = "2632" AND numero_chrono_annee = "2013" limit 1); 

    SELECT 
    CAST(concat(a.prefixe_numero,a.numero, " - ", a.date_ouverture) AS CHAR) as 'noet', 


    a.intitule as 'intitule', 

    (select nom from gta_geoptima_data.client as m where a.client_oid = m.oid) as 'no_client', 
    (select numero_client_abreviation from gta_geoptima_data.client as m where a.client_oid = m.oid) as 'sigle', 
    (select nom from gta_geoptima_data.client as m where a.client_oid = m.oid) as 'nom_raison', 
    (select intitule from gta_geoptima_data.direction_interne as m where a.direction_oid = m.oid) as 'service', 
    a.date_livraison as 'livraison' 
    FROM gta_geoptima_data.dossier as a 
    WHERE a.oid = (select dossier_oid from courrier_concerne_dossier where courrier_oid = param_oid); 

END$$ 

call courrier_envoye_pickdossiers(null); 

子查詢返回多個1行0.032秒

+1

好吧,只需調整SELECT在WHERE子句中使用,因此它總是返回1行。考慮「LIMIT」,「DISTINCT」或其他邏輯,具體取決於返回的數據和數據。 –

+0

也許我應該使用EXISTS? – boski

回答

0

將其更改爲做連接,而不是大量的子查詢: -

SELECT 
CAST(concat(a.prefixe_numero,a.numero, " - ", a.date_ouverture) AS CHAR) AS 'noet', 


a.intitule AS 'intitule', 

m.nom AS 'no_client', 
m.numero_client_abreviation AS 'sigle', 
m.nom AS 'nom_raison', 
n.intitule AS 'service', 
a.date_livraison AS 'livraison' 
FROM gta_geoptima_data.dossier AS a 
INNER JOIN 
(
    SELECT MAX(dossier_oid) AS max_dossier_oid 
    FROM courrier_concerne_dossier 
    WHERE courrier_oid = param_oid 
) sub1 
ON a.oid = sub1.max_dossier_oid 
LEFT OUTER JOIN gta_geoptima_data.client m 
ON a.client_oid = m.oid 
LEFT OUTER JOIN gta_geoptima_data.direction_interne n 
ON a.direction_oid = n.oid 

有一個子查詢,和我剛剛使用MAX來獲得最高匹配的dossier_oid。可以使用MIN或LIMIT 1或其他任何數字將數量減少到1.或者不打擾並返回所有匹配的數據(通過將WHERE a.oid = (更改爲WHERE a.oid IN (,您可以在原始SQL中執行此操作)

相關問題