子查询在MySQL where子句中返回多行

时间:2014-03-26 12:43:16

标签: mysql sql

我在MySQL程序中有错误,因为我试图在' Where'中选择多行。参数。当我把'Where = param_oid' (档案表行的oid),但我想从档案中返回几行(所以不要为表格courrier_concerne_dossier中存在的档案中的每个oid调用程序)无论如何 - 我正试图找到解决方案。

有没有简单的方法来解决这个问题?

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 个答案:

答案 0 :(得分: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中执行此操作)