如何使用很多子查询来改善/简化查询?

时间:2019-07-03 19:09:41

标签: mysql sql database subquery

我有一个sql查询,它对数据库中的某些寄存器求和并计数,问题是我使用了很多子查询,并且所有子查询都具有相同的条件,我需要找到更好的解决方案,因为这会导致我的系统运行缓慢。

我尝试使用简单的左联接,但是mysql返回了一行,我希望该值可以计数并求和表dbgeneralesImportacion上的每个寄存器。

我有这样的东西:

SELECT  dbgeneralesImportacion.id,
        sapito.dbgeneralesImportacion.documentosCompletos AS 'documentosCompletos', 
        (SELECT COUNT(c.id) 
        FROM sapito.dbcontenedores c 
        WHERE c.operacion = dbgeneralesImportacion.id ) AS 'ContadorContenedores', 
        (SELECT SUM(IF(c.estatus = 1, 1, 0)) 
        FROM sapito.dbcontenedores c 
        WHERE c.operacion = dbgeneralesImportacion.id ) AS 'ContadorDespachos', 
        (SELECT SUM(IF(c.estatus = 2, 1, 0)) 
        FROM sapito.dbcontenedores c 
        WHERE c.operacion = dbgeneralesImportacion.id ) AS 'ContadorCompletado', 
        (SELECT SUM(IF(c.desconsolidacion = 1, 1, 0)) 
        FROM sapito.dbcontenedores c 
        WHERE c.operacion = dbgeneralesImportacion.id ) AS 'DesconsolidacionPuerto',
        (SELECT SUM(c.bultos) FROM sapito.dbcontenedores c WHERE c.operacion = dbgeneralesImportacion.id)  AS 'bultos'
FROM    dbgeneralesImportacion

And my idea was to do this but counts all register and return a single row:

SELECT  dbgeneralesImportacion.id AS 'id',
        COUNT(c.id) AS 'ContadorContenedores', 
        SUM(IF(c.estatus = 1, 1, 0))  AS 'ContadorDespachos', 
        SUM(IF(c.estatus = 2, 1, 0))  AS 'ContadorCompletado', 
        SUM(IF(c.desconsolidacion = 1, 1, 0))  AS 'DesconsolidacionPuerto',
        SUM(c.bultos)  AS 'bultos'
FROM    dbgeneralesImportacion
        LEFT JOIN dbcontenedores c ON c.operacion = dbgeneralesImportacion.id

Thank you everyone

3 个答案:

答案 0 :(得分:2)

您可以使用

加入a和group
     SELECT  a.id
         , a.documentosCompletos
         , COUNT(c.id) ContadorContenedores
         , SUM(IF(c.estatus = 1, 1, 0)) ContadorDespachos
         , SUM(IF(c.estatus = 2, 1, 0)) ContadorCompletado
         , SUM(IF(c.desconsolidacion = 1, 1, 0)) DesconsolidacionPuerto
         ,  SUM(c.bultos) bultos

     FROM    dbgeneralesImportacion a
     INNER JOIN sapito.dbcontenedores c ON c.operacion = a.id
     GRUP BY a.id, a.documentosCompletos

答案 1 :(得分:1)

不好,但是你不能做这样的事

SELECT  dbgeneralesImportacion.id,
sapito.dbgeneralesImportacion.documentosCompletos AS 'documentosCompletos', 
COUNT(c.id)  AS 'ContadorContenedores', 
SUM(IF(c.estatus = 1, 1, 0)) AS 'ContadorDespachos', 
SUM(IF(c.estatus = 2, 1, 0)) AS 'ContadorCompletado', 
SUM(IF(c.desconsolidacion = 1, 1, 0)) AS 'DesconsolidacionPuerto',
SUM(c.bultos) AS 'bultos'

FROM    dbgeneralesImportacion, sapito.dbcontenedores c

WHERE c.operacion = dbgeneralesImportacion.id 
GROUP BY dbgeneralesImportacion.id

答案 2 :(得分:0)

您需要一个GROUP BY,但您也可以简化查询:

SELECT gi.id AS id,
       COUNT(c.id) AS ContadorContenedores, 
       SUM( c.estatus = 1 )  AS ContadorDespachos, 
       SUM( c.estatus = 2 )  AS ContadorCompletado, 
       SUM( c.desconsolidacion = 1 )  AS DesconsolidacionPuerto,
       SUM(c.bultos)  AS bultos
FROM dbgeneralesImportacion LEFT JOIN
     dbcontenedores c 
     ON c.operacion = gi.id
GROUP BY gi.id;

MySQL将布尔值视为数字上下文中的数字,true为1,false为0。这使得计数匹配值变得容易。

此外,仅对字符串和日期常量使用单引号。将它们用于列别名可能会导致难以调试的问题。