0

我有一个从 SQL Server 迁移到 MySQL 的数据库。我在 SQL Server 中有一个现有查询来获取关系不为空的列的值,我的意思是,如果两个表之间的关系为空,那么这意味着应该与另一个表存在关系。

这是我使用的查询:

SELECT C.expediente, 
       C.status, 
       Sum(M.monto)                             AS monto, 
       Sum(M.interes)                           AS interes, 
       Sum(M.iva)                               AS iva, 
       Sum(M.capital)                           AS capital, 
       M.fecha_mov, 
       AB.tipo_abono, 
       AB.id_deposito, 
       Isnull(Min(tg.nombre), Min(tp.nombcomp)) AS nombreGrupo 
FROM   movimientos AS M 
       JOIN acreditados AS A 
         ON A.id_acreditado = M.id_acreditado 
       JOIN creditos AS C 
         ON C.id_credito = A.id_credito 
       JOIN abonos AS AB 
         ON AB.id_movimiento = M.id_movimiento 
       OUTER apply (SELECT TOP 1 G.nombre 
                    FROM   grupos G 
                    WHERE  G.id_credito = C.id_credito) tg 
       OUTER apply (SELECT TOP 1 P.nombres + ' ' + P.apellido_paterno + ' ' 
                                 + P.apellido_materno AS NombComp 
                    FROM   personas P 
                    WHERE  A.id_persona = P.id_persona) tp 
GROUP  BY M.fecha_mov, 
          AB.tipo_abono, 
          AB.id_deposito, 
          C.expediente, 
          C.status 
ORDER  BY M.fecha_mov 

但似乎 MySQL 没有OUTER APPLYor ISNULL。如何将此查询转换为 MySQL?

4

2 回答 2

1

您有几个问题需要审查:

1 - APPLYMySQL 不支持该运算符。但是,对于您给定的用法,看起来您可能只使用LEFT OUTER JOIN.

2-MySQL 不支持TOP。相反,使用LIMIT. 但是,我认为在这种情况下您不需要它,因为您正在使用MIN这些字段中的每一个。

3 - 要在 MySQL 中连接字符串,请使用CONCATvs "+"

4 - 最后,我更喜欢COALESCE在 MySQL 中使用来检查NULLs. 不过还有其他选择。

所以把它们放在一起,这应该很接近(未经测试):

SELECT C.expediente, 
       C.status, 
       Sum(M.monto)                             AS monto, 
       Sum(M.interes)                           AS interes, 
       Sum(M.iva)                               AS iva, 
       Sum(M.capital)                           AS capital, 
       M.fecha_mov, 
       AB.tipo_abono, 
       AB.id_deposito, 
       COALESCE(MIN(tg.nombre), MIN(tp.nombcomp)) AS nombreGrupo 
FROM   movimientos AS M 
       JOIN acreditados AS A 
         ON A.id_acreditado = M.id_acreditado 
       JOIN creditos AS C 
         ON C.id_credito = A.id_credito 
       JOIN abonos AS AB 
         ON AB.id_movimiento = M.id_movimiento 
       LEFT OUTER JOIN (
            SELECT id_credito, nombre 
            FROM   grupos
            ) tg ON tg.id_credito = C.id_credito 
       LEFT OUTER JOIN (
            SELECT id_persona, CONCAT(nombres,' ',apellido_paterno,' ',apellido_materno) AS NombComp 
            FROM   personas 
            ) tp ON A.id_persona = tp.id_persona
GROUP  BY M.fecha_mov, 
          AB.tipo_abono, 
          AB.id_deposito, 
          C.expediente, 
          C.status 
ORDER  BY M.fecha_mov 

您可能不需要其中的一些字段GROUP BY——只DISTINCT需要您的字段。

于 2013-04-03T00:38:12.097 回答
1

我认为这会将其变成有效的 MySQL 查询:

SELECT C.expediente, C.status, Sum(M.monto) AS monto, Sum(M.interes) as interes, 
       Sum(M.iva) as iva, Sum(M.capital) as capital, 
       M.fecha_mov, AB.tipo_abono, AB.id_deposito, 
       coalesce(Min(tg.nombre), Min(tp.nombcomp)) AS nombreGrupo 
FROM   movimientos  M join
       acreditados A 
       ON A.id_acreditado = M.id_acreditado join
       creditos C 
       ON C.id_credito = A.id_credito join
       abonos AB 
       ON AB.id_movimiento = M.id_movimiento join
       (select g.id_credito, max(g.nombre) -- arbitrary nombre
        from grupos g
        group by g.id_credito
       ) tg
       on tG.id_credito = C.id_credito join
       (SELECT P.id_persona, concat_ws(' ', P.nombres, P.apellido_paterno, P.apellido_materno) AS NombComp 
        FROM   personas P 
        group by p.id_persona
       ) tp
       on A.id_persona = tP.id_persona
GROUP  BY M.fecha_mov, AB.tipo_abono, AB.id_deposito, C.expediente, C.status 
ORDER  BY M.fecha_mov 

我将其替换为isnull()--coalesce()如果您要在数据库之间切换,请尽可能使用标准 SQL。我将+for 连接替换为concat_ws(). 注意有一个小的区别。. . 如果其中一个字段为 NULL,则 MySQL 值会将其视为 ''。

我用cross apply聚合替换了。您在top 1没有 的情况下使用order by,因此这会获取任意行。所以,这个版本任意选择最大值。

于 2013-04-03T01:23:00.433 回答