使查询适应JDatabase

时间:2014-02-10 17:08:33

标签: php sql joomla

如何将以下查询调整为 JDatabase

SELECT
     c.RAZONSOCIAL                                      usu_razonSocial
    ,c.NIT                                              usu_nit
    ,c.SEDE                                             usu_sede
    ,c.EMAIL                                            usu_email
    ,IF(IFNULL(u.block, 1) = 0, 'Activo', 'Inactivo')   usu_estado
    ,COUNT(ct.ctf_id)                                   nroCertificados
FROM
    cargacliente c

    INNER JOIN 
    cargas 
    ON (crg_id = cargas_id) 
        AND (crg_status = 'Ok')

    INNER JOIN 
    certificados ct 
    ON (ctf_sede = SEDE)
        AND (ctf_nit = NIT)

    LEFT JOIN 
    database_1.bml_users u 
    ON (id = user_id)
GROUP BY
     c.NIT
    ,c.SEDE
ORDER BY
     usu_razonSocial
    ,usu_nit
    ,usu_sede;

这是Joomla 2.5.4。

我已阅读 this post ,但我无法做到。

我试着这样做:

<?php

$query
    ->select($db->quoteName(array('c.RAZONSOCIAL AS usu_razonSocial', 'c.NIT usu_nit', 'c.SEDE usu_sede', 'c.EMAIL usu_email', 'IF(IFNULL(u.block, 1) = 0, \'Activo\', \'Inactivo\') usu_estado', 'COUNT(ct.ctf_id) nroCertificados')))

    ->from($db->quoteName('database_2.cargacliente', 'c'))

    ->join('INNER', $db->quoteName('database_2.cargas','a') . ' ON (' . $db->quoteName('a.crg_id') . ' = ' . $db->quoteName('database_2.cargacert.cargas_id') . ') AND (' . $db->quoteName('a.crg_status') . ' = \'Ok\')')

    ->join('INNER', $db->quoteName('database_2.certificados','b') . ' ON (' . $db->quoteName('b.ctf_sede') . ' = ' . $db->quoteName('database_2.cargacliente.SEDE') . ') AND (' . $db->quoteName('b.ctf_nit') . ' = NIT)')

    ->join('LEFT', $db->quoteName('joomla_database.bml_users', 'u') . ' ON (' . $db->quoteName('database_2.usuario.usu_id') . ' = ' . $db->quoteName('joomla_database.bml_users.user_id') . ')')

    ->group(array('c.NIT', 'c.SEDE'))
    ->order(array('database_2.usuario.usu_razonSocial', 'database_2.usuario.usu_nit', 'database_2.usuario.usu_sede'));

?>

显示的错误如下:

500 - Ha ocurrido un error.
Unknown column 'c.RAZONSOCIAL usu_razonSocial' in 'field list' SQL=SELECT `c`.`RAZONSOCIAL usu_razonSocial`,`c`.`NIT usu_nit`,`c`.`SEDE usu_sede`,`c`.`EMAIL usu_email`,`IF(IFNULL(u`.`block, 1) = 0, 'Activo', 'Inactivo') usu_estado`,`COUNT(ct`.`ctf_id) nroCertificados` FROM `biochemical`.`cargacliente` AS `c` INNER JOIN `biochemical`.`cargas` AS `a` ON (`a`.`crg_id` = `biochemical`.`cargacert`.`cargas_id`) AND (`a`.`crg_status` = 'Ok') INNER JOIN `biochemical`.`certificados` AS `b` ON (`b`.`ctf_sede` = `biochemical`.`cargacliente`.`SEDE`) AND (`b`.`ctf_nit` = NIT) LEFT JOIN `biochemical_bml`.`bml_users` AS `u` ON (`biochemical`.`usuario`.`usu_id` = `biochemical_bml`.`bml_users`.`user_id`) GROUP BY c.NIT,c.SEDE ORDER BY biochemical.usuario.usu_razonSocial,biochemical.usuario.usu_nit,biochemical.usuario.usu_sede

2 个答案:

答案 0 :(得分:1)

变化如下:

  1. 删除SELECT语句中的第一个$ db-&gt; quoteName()。
  2. 从表格修改ALIAS。
  3. LEFT JOIN修改。
  4. 最终代码如下:

    $query
        ->select(array('c.RAZONSOCIAL AS usu_razonSocial', 'c.NIT AS usu_nit', 'c.SEDE AS usu_sede', 'c.EMAIL AS usu_email', 'IF(IFNULL(u.block, 1) = 0, \'Activo\', \'Inactivo\') AS usu_estado', 'COUNT(b.ctf_id) AS nroCertificados'))
    
        ->from($db->quoteName('database_2.cargacliente', 'c'))
    
        ->join('INNER', $db->quoteName('database_2.cargas','a') . ' ON (' . $db->quoteName('a.crg_id') . ' = ' . $db->quoteName('c.cargas_id') . ') AND (' . $db->quoteName('a.crg_status') . ' = \'Ok\')')
    
        ->join('INNER', $db->quoteName('database_2.certificados','b') . ' ON (' . $db->quoteName('b.ctf_sede') . ' = ' . $db->quoteName('c.SEDE') . ') AND (' . $db->quoteName('b.ctf_nit') . ' = c.NIT)')
    
        ->join('LEFT', $db->quoteName('joomla_database.bml_users', 'u') . ' ON (' . $db->quoteName('c.user_id') . ' = ' . $db->quoteName('u.id') . ')')
    
        ->group(array('c.NIT', 'c.SEDE'))
        ->order(array('usu_razonSocial', 'usu_nit', 'usu_sede'));
    

答案 1 :(得分:0)

如果没有合适的环境,这有点难以说明问题是什么。 我建议通过驱动程序调试,看看它失败了。 您当然也可以通过“setQuery”方法直接查询。但它只与MySQL兼容。

但是,请再次更新您的Joomla!尽快到最新版本。