在一个请求中将多个SELECT查询与同一个表中的不同条件组合在一起

时间:2016-01-05 10:13:23

标签: mysql sql

我只需要在一个请求中合并多个请求。所有请求都使用相同的表,但它们使用不同的条件。

  • 我尝试使用“UNION”关键字来合并请求,但它不起作用,因为每个SELECT中的字段数量都不相同。
  • 我看一下“JOIN”关键字但是,我看不到哪些字段可以进行加入

此请求的目的是在一行中获取总和的20个结果以填充我的UI中的表,并避免我的代码中的多个请求。

有办法做到这一点吗?

以下是我对此请求的测试之一

    SELECT IFNULL(SUM(CASE WHEN RecepPoste_id = 1 THEN Nombreplis END),0) AS NbrTSAPetits, 
      IFNULL(SUM(CASE WHEN RecepPoste_id = 2 THEN NombrePlis END),0) AS NbrTSAGrands, 
      IFNULL(SUM(CASE WHEN RecepPoste_id = 3 THEN NombrePlis END),0) AS NbrRecommandes, 
      IFNULL(SUM(CASE WHEN RecepPoste_id = 4 THEN NombrePlis END),0) AS NbrChronoBAL, 
      IFNULL(SUM(CASE WHEN RecepAccueil_id = 1 THEN Poids END),0) AS PoidsTotal, 
      IFNULL(SUM(CASE WHEN RecepAccueil_id = 1 THEN NombrePlis END),0) AS PlisTheoriques FROM datas WHERE Chantier_id = 1
UNION                 
SELECT IFNULL(SUM(CASE WHEN Scenario_id = 1 THEN NombrePlis END),0) AS NbrARR, 
       IFNULL(SUM(CASE WHEN Scenario_id = 2 THEN NombrePlis END),0) AS NbrSOR, 
       IFNULL(SUM(CASE WHEN Scenario_id = 3 THEN NombrePlis END),0) AS NbrTRT FROM datas WHERE Chantier_id = 2                                
UNION                     
SELECT IFNULL(SUM(NombrePlis),0) FROM Datas WHERE chantier_id = 3
UNION             
SELECT IFNULL(SUM(CASE WHEN Reliquat_id = 1 THEN Poids END),0) AS NbrARRNP, 
      IFNULL(SUM(CASE WHEN Reliquat_id = 2 THEN Poids END),0) AS NbrSTNP, 
      IFNULL(SUM(CASE WHEN Reliquat_id = 3 THEN NombreLots END),0) AS NbrARR, 
      IFNULL(SUM(CASE WHEN Reliquat_id = 4 THEN NombreLots END),0) AS ST, 
      IFNULL(SUM(CASE WHEN Reliquat_id = 5 THEN NombreLots END),0) AS NbrCLASS, 
      IFNULL(SUM(CASE WHEN Reliquat_id = 6 THEN NombreLots END),0) AS NbrINDEX FROM datas WHERE Chantier_id = 4
UNION             
SELECT IFNULL(SUM(CASE WHEN Retour_id = 1 THEN NombrePlis END),0) AS RETPapier, 
          IFNULL(SUM(CASE WHEN Retour_id = 2 THEN NombrePlis END),0) AS Divers, 
          IFNULL(SUM(CASE WHEN Retour_id = 3 THEN NombrePlis END),0) AS DTRSA, 
          IFNULL(SUM(CASE WHEN Retour_id = 4 THEN NombrePlis END),0) AS Originaux FROM datas WHERE Chantier_id = 5)

WHERE caf_id = 1;

我需要为每个请求指定chantier_id

谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

在所有查询中使用相同数量的列的最简单方法。您可以使用null“填充”某些查询中的列:

SELECT a, b, c FROM tableA
union all
SELECT d, e, null FROM tableB

因此,即使第二个查询只有2个“真实”列,整个查询也能正常工作。

另外一件事,你可能想要使用union ALL而不是union。它可能不太可能,但使用union将对不同的查询做出明确的分析。因此,如果来自不同查询的行相同,则会将其删除。

答案 1 :(得分:0)

最后,我找到了这个解决方案:

SELECT    IFNULL(SUM(CASE WHEN RecepPoste_id = 1 AND chantier_id = 1 THEN Nombreplis END),0) AS NbrTSAPetits,       --1
          IFNULL(SUM(CASE WHEN RecepPoste_id = 2 AND chantier_id = 1 THEN NombrePlis END),0) AS NbrTSAGrands,       --2
          IFNULL(SUM(CASE WHEN RecepPoste_id = 3 AND chantier_id = 1 THEN NombrePlis END),0) AS NbrRecommandes,     --3 
          IFNULL(SUM(CASE WHEN RecepPoste_id = 4  AND chantier_id = 1 THEN NombrePlis END),0) AS NbrChronoBAL,      --4
          IFNULL(SUM(CASE WHEN RecepAccueil_id = 1 AND chantier_id = 1 THEN Poids END),0) AS PoidsTotal,            --5
          IFNULL(SUM(CASE WHEN RecepAccueil_id = 1 AND chantier_id = 1 THEN NombrePlis END),0) AS PlisTheoriques,   --6       
          IFNULL(SUM(CASE WHEN Scenario_id = 1 AND chantier_id = 2THEN NombrePlis END),0) AS NbrARR,                --7
          IFNULL(SUM(CASE WHEN Scenario_id = 2 AND chantier_id = 2 THEN NombrePlis END),0) AS NbrSOR,               --8
          IFNULL(SUM(CASE WHEN Scenario_id = 3 AND chantier_id = 2 THEN NombrePlis END),0) AS NbrTRT,               --9
          IFNULL(SUM(CASE WHEN chantier_id = 3 THEN NombrePlis END),0) AS NbrPlisNumerise,                          --10
          IFNULL(SUM(CASE WHEN Reliquat_id = 1 AND chantier_id = 4 THEN Poids END),0) AS NbrARRNP,                  --11
          IFNULL(SUM(CASE WHEN Reliquat_id = 2 AND chantier_id = 4 THEN Poids END),0) AS NbrSTNP,                   --12
          IFNULL(SUM(CASE WHEN Reliquat_id = 3 AND chantier_id = 4THEN NombreLots END),0) AS NbrARR,                --13
          IFNULL(SUM(CASE WHEN Reliquat_id = 4 AND chantier_id = 4THEN NombreLots END),0) AS ST,                    --14
          IFNULL(SUM(CASE WHEN Reliquat_id = 5 AND chantier_id = 4THEN NombreLots END),0) AS NbrCLASS,              --15
          IFNULL(SUM(CASE WHEN Reliquat_id = 6 AND chantier_id = 4THEN NombreLots END),0) AS NbrINDEX,              --16
          IFNULL(SUM(CASE WHEN Retour_id = 1 THEN NombrePlis END),0) AS RETPapier,                                  --17
          IFNULL(SUM(CASE WHEN Retour_id = 2 AND chantier_id = 5THEN NombrePlis END),0) AS Divers,                  --18
          IFNULL(SUM(CASE WHEN Retour_id = 3 AND chantier_id = 5THEN NombrePlis END),0) AS DTRSA,                   --19
          IFNULL(SUM(CASE WHEN Retour_id = 4 AND chantier_id = 5THEN NombrePlis END),0) AS Originaux                --20
FROM Datas WHERE caf_id = 1;