将超大规模查询从 SQL Server 转换为 MySQL - JOIN 问题

Posted

技术标签:

【中文标题】将超大规模查询从 SQL Server 转换为 MySQL - JOIN 问题【英文标题】:Convert hyper-massive query from SQL Server to MySQL - Problems with a JOIN 【发布时间】:2011-12-28 07:31:06 【问题描述】:

我有一个在 SQL Server 2008 中运行良好的查询,但是当我尝试将它与 mysql 一起使用时,JOIN 似乎有问题。

第一个错误是

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 52 行的“ON Faxcont.IDDireccion = dir_cont.RecID AND Faxcont.Tipo = 2 AND FaxCont.Defecto”附近使用正确的语法

如何纠正?

SELECT     req.RecID, req.FechaEntrega, req.IDUsuario AS RecIDResp, req.NroSucursal, req_dp.NroFila, req.ID, req.ID AS NroReq, emp.Empresa AS NombreEmpresa, 
req.IDFiscalSistema, req_dp.ImporteUnitario1, req_dp.ImporteUnitario2, req_dp.ImporteUnitario3, req_dp.ImporteUnitario4, req_dp.ImporteUnitario5, 
req_dp.ImporteUnitario6, req.NroMoneda, req_dp.SobrePrecio, req_dp.Impuesto AS TasaIVA, dir_emp_sist.CodPais AS CodPaisEmpresa, 
dir_emp_sist.CodCiudad AS CodCiudadEmpresa, tel.numero AS TelefonoEmpresa, fax.numero AS FaxEmpresa, mail_emp.Direccion AS MailEmpresa, 
emp_sist.SitioWeb AS WebEmpresa, dir_cont.CodPais AS CodPaisCont, dir_cont.CodCiudad AS CodCiudadCont, telcont.numero AS TelefonoCont, 
faxcont.numero AS FaxCont, req.FechaModificacion AS FechaGrabacion, cont.Apellido AS ApellidoCont, cont.Nombre AS NombreCont, mail_cont.Direccion AS MailCont, 
emp_sist.Empresa AS NombreEmpSist, fis_sist.RazonSocial AS RazonSocialSist, fis_sist.NroImpuesto1 AS CUIT, dir_fis_sist.Calle AS CalleFisSist, 
dir_fis_sist.Numero AS NroFisSist, dir_fis_sist.Piso AS PisoFisSist, dir_fis_sist.Dto AS DtoFisSist, dir_fis_sist.CP AS CPFisSist, dir_fis_sist.Localidad AS LocFisSist, 
dir_fis_sist.Ciudad AS CiudadFisSist, dir_fis_sist.Provincia AS ProvinciaFisSist, dir_fis_sist.Pais AS PaisFisSist, responsable.Nombre AS NombreResp, 
responsable.Apellido AS ApellidoResp, mail_resp.Direccion AS MailResp, dir_resp.CodPais AS CodPaisResp, dir_resp.CodCiudad AS CodCiudadResp, 
telresp.numero AS TelefonoResp, com.DireccionEntrega AS DirEntrega, CASE PATINDEX('%.%', req_dp.NroItem) WHEN 0 THEN '0' WHEN 2 THEN '1' END AS Item, 
CASE PATINDEX('%.%', req_dp.NroItem) WHEN 0 THEN req_dp.NroItem WHEN 2 THEN LEFT(req_dp.NroItem, 1) END AS Item2, req_dp.NroItem, req_dp.Codigo, 
req_dp.Descripcion, CASE WHEN req_dp.recid IS NULL 
THEN 0 WHEN req_dp.Estado = 9 THEN 0 ELSE (CASE mon.Numero WHEN 1 THEN req_dp.ImporteUnitario1 WHEN 2 THEN req_dp.ImporteUnitario2 WHEN 3 THEN req_dp.ImporteUnitario3
WHEN 4 THEN req_dp.ImporteUnitario4 WHEN 5 THEN req_dp.ImporteUnitario5 WHEN 6 THEN req_dp.ImporteUnitario6 END) * ((100 - req_dp.Descuento) / 100) 
* ((100 + req_dp.SobrePrecio) / 100) END AS PrecioU, req_dp.Cantidad, 
(CASE mon.Numero WHEN 1 THEN req_dp.importeprecio1 WHEN 2 THEN req_dp.importeprecio2 WHEN 3 THEN req_dp.importeprecio3 WHEN 4 THEN req_dp.importeprecio4
WHEN 5 THEN req_dp.importeprecio5 WHEN 6 THEN req_dp.importeprecio6 END) AS Importe, req_dp.Descuento AS DescProd, 
CASE WHEN req_dp.Descuento = 0 THEN 0 ELSE ((CASE mon.Numero WHEN 1 THEN req_dp.importeprecio1 WHEN 2 THEN req_dp.importeprecio2 WHEN 3 THEN req_dp.importeprecio3
WHEN 4 THEN req_dp.importeprecio4 WHEN 5 THEN req_dp.importeprecio5 WHEN 6 THEN req_dp.importeprecio6 END) 
- ((req_dp.Descuento * (CASE mon.Numero WHEN 1 THEN req_dp.importeprecio1 WHEN 2 THEN req_dp.importeprecio2 WHEN 3 THEN req_dp.importeprecio3 WHEN 4
THEN req_dp.importeprecio4 WHEN 5 THEN req_dp.importeprecio5 WHEN 6 THEN req_dp.importeprecio6 END) / 100))) 
- (CASE mon.Numero WHEN 1 THEN req_dp.importeprecio1 WHEN 2 THEN req_dp.importeprecio2 WHEN 3 THEN req_dp.importeprecio3 WHEN 4 THEN req_dp.importeprecio4
WHEN 5 THEN req_dp.importeprecio5 WHEN 6 THEN req_dp.importeprecio6 END) END AS ImpDescXProd, req.SubTotal, req.Descuento AS PorcDescGral, 
req.SubTotal - req.SubTotal * ((100 - req.Descuento) / 100) AS DescuentoGral, req.SubTotal2 AS SubTotalConDto, req.Impuesto AS IVA, req.Total, mon.Simbolo, 
mon.Moneda, req.ID AS NroRequerimiento, req.MostrarImpuestos, 
CASE req.MostrarImpuestos WHEN 1 THEN 'Los valores están expresados en ' + mon.Moneda + '. Incluyen IVA.' WHEN 0 THEN 'Los valores están expresados en ' + mon.Moneda
+ '. No incluyen IVA.' END AS LeyendaIVA, com.CondicionesPago AS CondPago, com.FormaPago, com.PlazoEntrega, com.HorarioEntrega, com_t.Tipo AS TipoTransp, 
com_t.valor AS TranspPropio, emp_t.Empresa AS EmpTransp, fis_t.RazonSocial AS RazSocTransp, com.PrecioEnvio, adi.Campo1 AS Adi1, adi.Campo2 AS Adi2, 
adi.Campo3 AS Adi3, adi.Campo4 AS Adi4, adi.Campo5 AS Adi5, adi.Campo6 AS Adi6, adi.Campo7 AS Adi7, adi.Campo8 AS Adi8, adi.Campo9 AS Adi9, 
adi.Campo10 AS Adi10, adi.Campo11 AS Adi11, adi.Campo12 AS Adi12, adi.Campo13 AS Adi13, adi.Campo14 AS Adi14, adi.Campo15 AS Adi15, adi.Campo16 AS Adi16, 
adi.Campo17 AS Adi17, adi.Campo18 AS Adi18, adi.Campo19 AS Adi19, adi.Campo20 AS Adi20, ped_1.IDRequerimiento AS IDPed, ped2_1.RecID AS IDPed2, 
ped3.RecID AS IDPed3, req_dp.Notas, req.Descuento AS DesReq, req_dp.Descuento AS DescProd, cont.IDContacto AS RecidContReq, usu.RecID AS RecidContResp, 
direcciones.Calle, direcciones.Numero, direcciones.Piso, direcciones.Dto, direcciones.Torre, direcciones.CP, direcciones.Ciudad, direcciones.Provincia, 
direcciones.Pais, direcciones.Defecto AS Dir_Cont_Defecto, direcciones.RecID AS Dir_Cont_Recid
FROM         telefonos AS faxcont RIGHT OUTER JOIN
telefonos AS telcont RIGHT OUTER JOIN
direcciones AS dir_cont RIGHT OUTER JOIN
fiscal AS fis_sist INNER JOIN
requerimientos AS req INNER JOIN
requerimientositems AS req_dp ON req.RecID = req_dp.IDRequerimiento INNER JOIN
moneda AS mon ON req.NroMoneda = mon.Numero INNER JOIN
contactos AS cont ON req.IDRef = cont.IDContacto INNER JOIN
empresas AS emp ON cont.IDEmpresa = emp.IDEmpresa ON fis_sist.RecID = req.IDFiscalSistema INNER JOIN
empresas AS emp_sist ON fis_sist.IDref = emp_sist.IDEmpresa LEFT OUTER JOIN
direcciones ON emp.IDEmpresa = direcciones.idref LEFT OUTER JOIN
contactos AS responsable INNER JOIN
usuarios AS usu ON responsable.IDContacto = usu.IDContacto ON req.IDUsuario = usu.RecID LEFT OUTER JOIN
direccionescorreo AS mail_resp ON responsable.IDContacto = mail_resp.IDref AND mail_resp.Defecto = 1 LEFT OUTER JOIN
direcciones AS dir_emp_sist ON emp_sist.IDEmpresa = dir_emp_sist.idref AND dir_emp_sist.Defecto = 1 LEFT OUTER JOIN
direcciones AS dir_fis_sist ON fis_sist.IDDireccion = dir_fis_sist.RecID ON dir_cont.idref = cont.IDContacto ON telcont.IDDireccion = dir_cont.RecID AND 
telcont.Tipo = 3 AND telcont.Defecto = 1 ON faxcont.IDDireccion = dir_cont.RecID AND faxcont.Tipo = 2 AND faxcont.Defecto = 1 LEFT OUTER JOIN
direccionescorreo AS mail_cont ON cont.IDContacto = mail_cont.IDref AND mail_cont.Defecto = 1 LEFT OUTER JOIN
direcciones AS dir_resp ON responsable.IDContacto = dir_resp.idref AND dir_resp.Defecto = 1 LEFT OUTER JOIN
telefonos AS telresp ON responsable.IDContacto = telresp.IDref2 AND telresp.Tipo = 3 AND telresp.Defecto = 1 LEFT OUTER JOIN
comercial AS com ON req.RecID = com.idref LEFT OUTER JOIN
comercialtransportes AS com_t ON com.RecID = com_t.idref LEFT OUTER JOIN
empresas AS emp_t ON com_t.IDEmpresa = emp_t.IDEmpresa LEFT OUTER JOIN
fiscal AS fis_t ON emp_t.IDEmpresa = fis_t.IDref AND fis_t.Defecto = 1 LEFT OUTER JOIN
adicional AS adi ON req.RecID = adi.IDRef LEFT OUTER JOIN
telefonos AS tel ON dir_emp_sist.RecID = tel.IDDireccion AND tel.Tipo = 5 AND tel.Defecto = 1 LEFT OUTER JOIN
telefonos AS fax ON dir_emp_sist.RecID = fax.IDDireccion AND fax.Tipo = 7 AND fax.Defecto = 1 LEFT OUTER JOIN
direccionescorreo AS mail_emp ON emp_sist.IDEmpresa = mail_emp.IDref AND mail_emp.Defecto = 1 LEFT OUTER JOIN
(SELECT     IDRequerimiento
FROM          pedidos AS ped
GROUP BY IDRequerimiento) AS ped_1 ON req.RecID = ped_1.IDRequerimiento LEFT OUTER JOIN
(SELECT     RecID
FROM          pedidos AS ped2
GROUP BY RecID) AS ped2_1 ON req.IDPedido = ped2_1.RecID LEFT OUTER JOIN
requerimientositemsclientes AS req_dp_cli ON req_dp.RecID = req_dp_cli.IDReqProd LEFT OUTER JOIN
pedidositems AS ped_dp ON req_dp_cli.IDPedProd = ped_dp.RecID LEFT OUTER JOIN
pedidos AS ped3 ON ped_dp.IDPedido = ped3.RecID
WHERE     (req.RecID = '?ID')
ORDER BY req_dp.NroFila

编辑

在下面附加格式化代码,使用http://sqlformat.appspot.com:

SELECT req.RecID,
       req.FechaEntrega,
       req.IDUsuario AS RecIDResp,
       req.NroSucursal,
       req_dp.NroFila,
       req.ID,
       req.ID AS NroReq,
       emp.Empresa AS NombreEmpresa,
       req.IDFiscalSistema,
       req_dp.ImporteUnitario1,
       req_dp.ImporteUnitario2,
       req_dp.ImporteUnitario3,
       req_dp.ImporteUnitario4,
       req_dp.ImporteUnitario5,
       req_dp.ImporteUnitario6,
       req.NroMoneda,
       req_dp.SobrePrecio,
       req_dp.Impuesto AS TasaIVA,
       dir_emp_sist.CodPais AS CodPaisEmpresa,
       dir_emp_sist.CodCiudad AS CodCiudadEmpresa,
       tel.numero AS TelefonoEmpresa,
       fax.numero AS FaxEmpresa,
       mail_emp.Direccion AS MailEmpresa,
       emp_sist.SitioWeb AS WebEmpresa,
       dir_cont.CodPais AS CodPaisCont,
       dir_cont.CodCiudad AS CodCiudadCont,
       telcont.numero AS TelefonoCont,
       faxcont.numero AS FaxCont,
       req.FechaModificacion AS FechaGrabacion,
       cont.Apellido AS ApellidoCont,
       cont.Nombre AS NombreCont,
       mail_cont.Direccion AS MailCont,
       emp_sist.Empresa AS NombreEmpSist,
       fis_sist.RazonSocial AS RazonSocialSist,
       fis_sist.NroImpuesto1 AS CUIT,
       dir_fis_sist.Calle AS CalleFisSist,
       dir_fis_sist.Numero AS NroFisSist,
       dir_fis_sist.Piso AS PisoFisSist,
       dir_fis_sist.Dto AS DtoFisSist,
       dir_fis_sist.CP AS CPFisSist,
       dir_fis_sist.Localidad AS LocFisSist,
       dir_fis_sist.Ciudad AS CiudadFisSist,
       dir_fis_sist.Provincia AS ProvinciaFisSist,
       dir_fis_sist.Pais AS PaisFisSist,
       responsable.Nombre AS NombreResp,
       responsable.Apellido AS ApellidoResp,
       mail_resp.Direccion AS MailResp,
       dir_resp.CodPais AS CodPaisResp,
       dir_resp.CodCiudad AS CodCiudadResp,
       telresp.numero AS TelefonoResp,
       com.DireccionEntrega AS DirEntrega,
       CASE PATINDEX('%.%', req_dp.NroItem)
           WHEN 0 THEN '0'
           WHEN 2 THEN '1'
       END AS Item,
       CASE PATINDEX('%.%', req_dp.NroItem)
           WHEN 0 THEN req_dp.NroItem
           WHEN 2 THEN LEFT(req_dp.NroItem, 1)
       END AS Item2,
       req_dp.NroItem,
       req_dp.Codigo,
       req_dp.Descripcion,
       CASE
           WHEN req_dp.recid IS NULL THEN 0
           WHEN req_dp.Estado = 9 THEN 0
           ELSE (CASE mon.Numero
                     WHEN 1 THEN req_dp.ImporteUnitario1
                     WHEN 2 THEN req_dp.ImporteUnitario2
                     WHEN 3 THEN req_dp.ImporteUnitario3
                     WHEN 4 THEN req_dp.ImporteUnitario4
                     WHEN 5 THEN req_dp.ImporteUnitario5
                     WHEN 6 THEN req_dp.ImporteUnitario6
                 END) * ((100 - req_dp.Descuento) / 100) * ((100 + req_dp.SobrePrecio) / 100)
       END AS PrecioU,
       req_dp.Cantidad,
       (CASE mon.Numero
            WHEN 1 THEN req_dp.importeprecio1
            WHEN 2 THEN req_dp.importeprecio2
            WHEN 3 THEN req_dp.importeprecio3
            WHEN 4 THEN req_dp.importeprecio4
            WHEN 5 THEN req_dp.importeprecio5
            WHEN 6 THEN req_dp.importeprecio6
        END) AS Importe,
       req_dp.Descuento AS DescProd,
       CASE
           WHEN req_dp.Descuento = 0 THEN 0
           ELSE ((CASE mon.Numero WHEN 1 THEN req_dp.importeprecio1 WHEN 2 THEN req_dp.importeprecio2 WHEN 3 THEN req_dp.importeprecio3 WHEN 4 THEN req_dp.importeprecio4 WHEN 5 THEN req_dp.importeprecio5 WHEN 6 THEN req_dp.importeprecio6 END) - ((req_dp.Descuento * (CASE mon.Numero WHEN 1 THEN req_dp.importeprecio1 WHEN 2 THEN req_dp.importeprecio2 WHEN 3 THEN req_dp.importeprecio3 WHEN 4 THEN req_dp.importeprecio4 WHEN 5 THEN req_dp.importeprecio5 WHEN 6 THEN req_dp.importeprecio6 END) / 100))) - (CASE mon.Numero
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            WHEN 1 THEN req_dp.importeprecio1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            WHEN 2 THEN req_dp.importeprecio2
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            WHEN 3 THEN req_dp.importeprecio3
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            WHEN 4 THEN req_dp.importeprecio4
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            WHEN 5 THEN req_dp.importeprecio5
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            WHEN 6 THEN req_dp.importeprecio6
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        END)
       END AS ImpDescXProd,
       req.SubTotal,
       req.Descuento AS PorcDescGral,
       req.SubTotal - req.SubTotal * ((100 - req.Descuento) / 100) AS DescuentoGral,
                                     req.SubTotal2 AS SubTotalConDto,
                                     req.Impuesto AS IVA,
                                     req.Total,
                                     mon.Simbolo,
                                     mon.Moneda,
                                     req.ID AS NroRequerimiento,
                                     req.MostrarImpuestos,
                                     CASE req.MostrarImpuestos
                                         WHEN 1 THEN 'Los valores están expresados en ' + mon.Moneda + '. Incluyen IVA.'
                                         WHEN 0 THEN 'Los valores están expresados en ' + mon.Moneda + '. No incluyen IVA.'
                                     END AS LeyendaIVA,
                                     com.CondicionesPago AS CondPago,
                                     com.FormaPago,
                                     com.PlazoEntrega,
                                     com.HorarioEntrega,
                                     com_t.Tipo AS TipoTransp,
                                     com_t.valor AS TranspPropio,
                                     emp_t.Empresa AS EmpTransp,
                                     fis_t.RazonSocial AS RazSocTransp,
                                     com.PrecioEnvio,
                                     adi.Campo1 AS Adi1,
                                     adi.Campo2 AS Adi2,
                                     adi.Campo3 AS Adi3,
                                     adi.Campo4 AS Adi4,
                                     adi.Campo5 AS Adi5,
                                     adi.Campo6 AS Adi6,
                                     adi.Campo7 AS Adi7,
                                     adi.Campo8 AS Adi8,
                                     adi.Campo9 AS Adi9,
                                     adi.Campo10 AS Adi10,
                                     adi.Campo11 AS Adi11,
                                     adi.Campo12 AS Adi12,
                                     adi.Campo13 AS Adi13,
                                     adi.Campo14 AS Adi14,
                                     adi.Campo15 AS Adi15,
                                     adi.Campo16 AS Adi16,
                                     adi.Campo17 AS Adi17,
                                     adi.Campo18 AS Adi18,
                                     adi.Campo19 AS Adi19,
                                     adi.Campo20 AS Adi20,
                                     ped_1.IDRequerimiento AS IDPed,
                                     ped2_1.RecID AS IDPed2,
                                     ped3.RecID AS IDPed3,
                                     req_dp.Notas,
                                     req.Descuento AS DesReq,
                                     req_dp.Descuento AS DescProd,
                                     cont.IDContacto AS RecidContReq,
                                     usu.RecID AS RecidContResp,
                                     direcciones.Calle,
                                     direcciones.Numero,
                                     direcciones.Piso,
                                     direcciones.Dto,
                                     direcciones.Torre,
                                     direcciones.CP,
                                     direcciones.Ciudad,
                                     direcciones.Provincia,
                                     direcciones.Pais,
                                     direcciones.Defecto AS Dir_Cont_Defecto,
                                     direcciones.RecID AS Dir_Cont_Recid
FROM telefonos AS faxcont
RIGHT OUTER JOIN telefonos AS telcont
RIGHT OUTER JOIN direcciones AS dir_cont
RIGHT OUTER JOIN fiscal AS fis_sist
INNER JOIN requerimientos AS req
INNER JOIN requerimientositems AS req_dp ON req.RecID = req_dp.IDRequerimiento
INNER JOIN moneda AS mon ON req.NroMoneda = mon.Numero
INNER JOIN contactos AS cont ON req.IDRef = cont.IDContacto
INNER JOIN empresas AS emp ON cont.IDEmpresa = emp.IDEmpresa ON fis_sist.RecID = req.IDFiscalSistema
INNER JOIN empresas AS emp_sist ON fis_sist.IDref = emp_sist.IDEmpresa
LEFT OUTER JOIN direcciones ON emp.IDEmpresa = direcciones.idref
LEFT OUTER JOIN contactos AS responsable
INNER JOIN usuarios AS usu ON responsable.IDContacto = usu.IDContacto ON req.IDUsuario = usu.RecID
LEFT OUTER JOIN direccionescorreo AS mail_resp ON responsable.IDContacto = mail_resp.IDref
AND mail_resp.Defecto = 1
LEFT OUTER JOIN direcciones AS dir_emp_sist ON emp_sist.IDEmpresa = dir_emp_sist.idref
AND dir_emp_sist.Defecto = 1
LEFT OUTER JOIN direcciones AS dir_fis_sist ON fis_sist.IDDireccion = dir_fis_sist.RecID ON dir_cont.idref = cont.IDContacto ON telcont.IDDireccion = dir_cont.RecID
AND telcont.Tipo = 3
AND telcont.Defecto = 1 ON faxcont.IDDireccion = dir_cont.RecID
AND faxcont.Tipo = 2
AND faxcont.Defecto = 1
LEFT OUTER JOIN direccionescorreo AS mail_cont ON cont.IDContacto = mail_cont.IDref
AND mail_cont.Defecto = 1
LEFT OUTER JOIN direcciones AS dir_resp ON responsable.IDContacto = dir_resp.idref
AND dir_resp.Defecto = 1
LEFT OUTER JOIN telefonos AS telresp ON responsable.IDContacto = telresp.IDref2
AND telresp.Tipo = 3
AND telresp.Defecto = 1
LEFT OUTER JOIN comercial AS com ON req.RecID = com.idref
LEFT OUTER JOIN comercialtransportes AS com_t ON com.RecID = com_t.idref
LEFT OUTER JOIN empresas AS emp_t ON com_t.IDEmpresa = emp_t.IDEmpresa
LEFT OUTER JOIN fiscal AS fis_t ON emp_t.IDEmpresa = fis_t.IDref
AND fis_t.Defecto = 1
LEFT OUTER JOIN adicional AS adi ON req.RecID = adi.IDRef
LEFT OUTER JOIN telefonos AS tel ON dir_emp_sist.RecID = tel.IDDireccion
AND tel.Tipo = 5
AND tel.Defecto = 1
LEFT OUTER JOIN telefonos AS fax ON dir_emp_sist.RecID = fax.IDDireccion
AND fax.Tipo = 7
AND fax.Defecto = 1
LEFT OUTER JOIN direccionescorreo AS mail_emp ON emp_sist.IDEmpresa = mail_emp.IDref
AND mail_emp.Defecto = 1
LEFT OUTER JOIN
  (SELECT IDRequerimiento
   FROM pedidos AS ped
   GROUP BY IDRequerimiento) AS ped_1 ON req.RecID = ped_1.IDRequerimiento
LEFT OUTER JOIN
  (SELECT RecID
   FROM pedidos AS ped2
   GROUP BY RecID) AS ped2_1 ON req.IDPedido = ped2_1.RecID
LEFT OUTER JOIN requerimientositemsclientes AS req_dp_cli ON req_dp.RecID = req_dp_cli.IDReqProd
LEFT OUTER JOIN pedidositems AS ped_dp ON req_dp_cli.IDPedProd = ped_dp.RecID
LEFT OUTER JOIN pedidos AS ped3 ON ped_dp.IDPedido = ped3.RecID
WHERE (req.RecID = '?ID')
ORDER BY req_dp.NroFila

【问题讨论】:

天哪!太疯狂了 这一定是JOINs 的某种记录 我见过更糟糕的 ;) 虽然通常不会发布在公共网站上供其他人修复! 该死的地狱。这是参加比赛吗? :-) 【参考方案1】:

从前两个表开始,将它们连接在一起 -

requerimientos AS req INNER JOIN
requerimientositems AS req_dp ON req.RecID = req_dp.IDRequerimiento'

如果可行(编写一个涉及两个表的查询 [即测试] 并确保没问题)将下一个添加到 -

requerimientos AS req INNER JOIN
requerimientositems AS req_dp ON req.RecID = req_dp.IDRequerimiento INNER JOIN 
moneda AS mon ON req.NroMoneda = mon.Numero INNER JOIN
contactos AS cont ON req.IDRef = cont.IDContacto

然后重复。


这可能需要一点努力,但 这是一种敏捷方法,将极大地帮助您前进。 '特别是那种意大利面;)

如果可行和/或您想尝试更大的跳跃,请尝试仅删除这些子选择,看看是否是问题所在: 删除:

LEFT OUTER JOIN
  (SELECT IDRequerimiento
   FROM pedidos AS ped
   GROUP BY IDRequerimiento) AS ped_1 ON req.RecID = ped_1.IDRequerimiento
LEFT OUTER JOIN
  (SELECT RecID
   FROM pedidos AS ped2
   GROUP BY RecID) AS ped2_1 ON req.IDPedido = ped2_1.RecID  

最后,根据实际的错误提示,看这里:!!!

AND telcont.Tipo = 3
AND telcont.Defecto = 1 ON faxcont.IDDireccion = dir_cont.RecID
AND faxcont.Tipo = 2

没有那个ON的表?

【讨论】:

现在你必须完成查询只是为了好玩!请帮帮我! 太好了,谢谢!我已经给你一个关于 ON 的提示,但你没有表示任何后续行动。我们在 SO 提供指导,但我们不会重新编写这样的大量查询。这种水平的东西通常是有偿的(成本也相当高)。我唯一能真正为您和其他海报提供(诚实地尝试帮助)的是sqlize.com,人们可以在那里尝试查询。这就像jsfiddle。您甚至可以创建表格!

以上是关于将超大规模查询从 SQL Server 转换为 MySQL - JOIN 问题的主要内容,如果未能解决你的问题,请参考以下文章

将查询从 SQL Server 转换为 Access 2000

将 SQL Server 存储过程转换为 Oracle 过程以从表中查询

将 SQL Server 查询转换为 Linq 查询

将 Access 交叉表查询转换为 SQL Server

MS Access 查询转换为 Sql Server

使用 LIKE 将 Oracle 查询转换为 SQL Server 查询