Adapting query to JDatabase - php

How can I adapt the following query to 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;
This is Joomla 2.5.4.
I have read this post but I couldn't do it.
I tried to do it in this way:
<?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'));
?>
The error shown is the following:
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

The changes were the following:
Remove the first $db->quoteName() in the SELECT statement.
Modify ALIAS from tables.
LEFT JOIN modification.
The final code is the following:
$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'));

This is a bit difficult to say what the problem is without having the right environment.
I would suggest to debug through the driver to see where it fails.
You can of course also put the query direct through the "setQuery" method. It would be however compatible with MySQL only.
But, once again, please update your Joomla! to the latest version ASAP.

Related

Combine multiple SQL queries into single query for speed

I am working in PHP with Laravel and the page I have been given contains 3 SQL queries on separate tables but using the same criteria. I have tried combining them but the combined results are slower than the original. What can I do to increase rather than decrease the speed of these queries?
Here are my queries:
$searchFor = 'debtor_name';
$searchForBKR = 'estate_name';
$searchForHypotech = 'hypleg_lot_credit_debtor_name';
$arraySearch = explode(",", $request->input('name'));
$search = $searchFor . ' like "%';
$bkrSearch = $searchForBKR . ' like "%';
$hypotechSearch = $searchForHypotech . ' like "%';
$searchCompany = $searchCompanyFor . ' like "%';
$searchPlaintiff = $searchPlaintiffFor . ' like "%';
$searchDefendantName = $searchDefendantNameFor . ' like "%';
$search = $search . $arraySearch[0] . '%"';
$bkrSearch = $bkrSearch . $arraySearch[0] . '%"';
$hypotechSearch = $hypotechSearch . $arraySearch[0] . '%"';
$searchCompany = $searchCompany . $arraySearch[0] . '%"';
$searchPlaintiff = $searchPlaintiff . $arraySearch[0] . '%"';
$searchDefendantName = $searchDefendantName . $arraySearch[0] . '%"';
for ($i = 1; $i < count($arraySearch); $i++) {
$search = $search . " or " . $searchFor . ' like "%' . $arraySearch[$i] . '%"';
$bkrSearch = $bkrSearch . " or " . $searchForBKR . ' like "%' . $arraySearch[$i] . '%"';
$hypotechSearch = $hypotechSearch . " or " . $searchForHypotech . ' like "%' . $arraySearch[$i] . '%"';
$searchCompany = $searchCompany . " or " . $searchCompanyFor . ' like "%' . $arraySearch[$i] . '%"';
$searchPlaintiff = $searchPlaintiff . " or " . $searchPlaintiffFor . ' like "%' . $arraySearch[$i] . '%"';
$searchDefendantName = $searchDefendantName . " or " . $searchDefendantNameFor . ' like "%' . $arraySearch[$i] . '%"';
}
$sis = DB::select('select debtor_name, count(*) as sis_total from equifax_sis_regions' . $search . ')
group by debtor_name');
$bkr = DB::select('select estate_name as debtor_name, count(*) as bkr_total from equifax_bkr_regions' . $bkrSearch . ')
group by estate_name');
$hypotech = DB::select('select hypleg_lot_credit_debtor_name as debtor_name, count(*) as hypotech_total from equifax_hypotech_regions' . $hypotechSearch . ')
group by hypleg_lot_credit_debtor_name');
I have tried replacing the 3 queries by using outer joins like this:
$other = 'select debtor_name, count(debtor_name) as sis_total,
estate_name as debtor_name, count(estate_name) as bkr_total
hypleg_lot_credit_debtor_name as debtor_name, count(hypleg_lot_credit_debtor_name) as hypotech_total
from equifax_sis_regions outer join equifax_bkr_regions on (equifax_sis_regions.debtor_name=estate_name)
outer join equifax_hypotech_regions on (equifax_sis_regions.debtor_name=hypleg_lot_credit_debtor_name)
WHERE (' . $search . ') OR ' . $bkrSearch . ') OR (' . $hypotechSearch . ')
GROUP BY debtor_name, estate_name, hypleg_lot_credit_debtor_name';
but that is a much slower query to run. The database contains over 4 million rows and the difference in API calls to the 2 query versions is ~53 seconds for the original and ~78 seconds for the combined form. Is there any way I can modify this combined query to be faster rather than slower?
Thanks in advance
You can use the UNION ALL operator to combine multiple SELECT statements. You could combine them like this:
SELECT debtor_name, SUM(sis_total), SUM(bkr_total), SUM(hypotech_total)
FROM (
SELECT debtor_name, COUNT(*) AS sis_total, 0 AS bkr_total, 0 AS hypotech_total
FROM equifax_sis_regions
-- add where clause here
GROUP BY debtor_name
UNION ALL
SELECT estate_name AS debtor_name, 0 AS sis_total, COUNT(*) AS bkr_total, 0 AS hypotech_total
FROM equifax_bkr_regions
-- add where clause here
GROUP BY estate_name
UNION ALL
SELECT hypleg_lot_credit_debtor_name AS debtor_name, 0 AS sis_total, 0 AS bkr_total, COUNT(*) AS hypotech_total
FROM equifax_hypotech_regions
-- add where clause here
GROUP BY hypleg_lot_credit_debtor_name
) derived
GROUP BY debtor_name;

Rewriting database query in Codeigniter's query builder

I'm trying to rewrite existing database query in Codeigniter' query builder, how do I solve WHERE EXISTS(I think that's where's the problem)?
This is original query I want to rewrite:
$query = $this->db->query('SELECT p_customer.*' .
' FROM p_customer' .
' WHERE EXISTS (' .
'SELECT null' .
' FROM p_customer_group_rel' .
' WHERE p_customer_group_rel.customer_group_id=' . $e_id.
' AND p_customer_group_rel.customer_id = p_customer.id' .
')' .
' AND p_customer.deleted IS NULL' .
' AND p_customer.id > 0' .
' ORDER BY p_customer.full_name'
);
This is what I got so far:
$query = $this->db
->select('p_customer.*')
->from('p_customer')
->where('EXISTS(SELECT null FROM p_customer_group_rel WHERE
p_customer_group_rel.customer_group_id= ' . $e_id . ' AND
p_customer_group_rel.customer_id = p_customer.id)')
->where('p_customer.deleted is NULL')
->where('p_customer.id > 0')
->order_by('p_customer.full_name');
->get();
The result I get from first query is array of object.
This is what I get from my query:
SELECT p_customer.* FROM p_customer WHERE EXISTS( SELECT null FROM p_customer_group_rel WHERE p_customer_group_rel.customer_group_id= $e_id ) AND p_customer_group.port_id = p_port.id
which is not what I want, any help? :)
Maybe it's not best solution, but I did the following and it works:
$sub_q = 'SELECT null FROM p_customer_group_rel WHERE p_customer_group_rel.customer_group_id=' . $e_id . ' AND p_customer_group_rel.customer_id = p_customer.id';
$query = $this->db ->select('p_customer.*') ->from('p_customer') ->where('EXISTS(' . $sub_q . ')') ->where('p_customer.id >', 0) ->where('p_customer.deleted', NULL) ->order_by('p_customer.full_name') ->get();

How to convert this MySQL query to Yii2 ActiveQuery format?

This is my MySQL query
SELECT dc.`dc_key`
,#delivered_qty := (SELECT IFNULL(SUM(dd.`qty`),0)
FROM `delivery_detail` dd
WHERE dd.`item_key` = 1
AND dd.dc_key = dc.dc_key
) AS delivered_qty
,#borrowed_qty := (SELECT IFNULL(SUM(bd.`qty`),0)
FROM `borrow_detail` bd
WHERE bd.`item_key` = 1
AND bd.`voided` = 0
AND bd.`returned` = 0
AND bd.`dc_key` = dc.`dc_key`
) AS borrowed_qty
, (#delivered_qty - #borrowed_qty) AS available_qty
FROM `delivery_catalog` dc
WHERE (#delivered_qty - #borrowed_qty) > 0
LIMIT 1
This is my result without the limit
This is what I have tried so far but to no good.
$SQL = ''
. 'SELECT dc.`dc_key` '
. ',#delivered_qty := (SELECT IFNULL(SUM(dd.`qty`),0) '
. ' FROM `delivery_detail` dd '
. ' WHERE dd.`item_key` = :item_key '
. ' AND dd.dc_key = dc.dc_key '
. ') AS delivered_qty '
. ',#borrowed_qty := (SELECT IFNULL(SUM(bd.`qty`),0) '
. ' FROM `borrow_detail` bd '
. ' WHERE bd.`item_key` = :item_key '
. ' AND bd.`voided` = 0 '
. ' AND bd.`returned` = 0 '
. ' AND bd.`dc_key` = dc.`dc_key` '
. ') AS borrowed_qty '
. ', (#delivered_qty - #borrowed_qty) AS available_qty '
. 'FROM `delivery_catalog` dc '
. 'WHERE (#delivered_qty - #borrowed_qty) > 0 '
//. 'LIMIT 1 '
. '';
$rawData = DeliveryDetail::findBySql($SQL, [':item_key' => $item_key])->ALL();
print_r($rawData);
return;
It returns an empty array.
I want to utilized the query builder of yii2 for a clean code.
$SQL = ''
. 'SELECT dc.`dc_key` '
. ',#delivered_qty := (SELECT IFNULL(SUM(dd.`qty`),0) '
. ' FROM `delivery_detail` dd '
. ' WHERE dd.`item_key` = :item_key '
. ' AND dd.dc_key = dc.dc_key '
. ') AS delivered_qty '
. ',#borrowed_qty := (SELECT IFNULL(SUM(bd.`qty`),0) '
. ' FROM `borrow_detail` bd '
. ' WHERE bd.`item_key` = :item_key '
. ' AND bd.`voided` = 0 '
. ' AND bd.`returned` = 0 '
. ' AND bd.`dc_key` = dc.`dc_key` '
. ') AS borrowed_qty '
. ', (#delivered_qty - #borrowed_qty) AS available_qty '
. 'FROM `delivery_catalog` dc '
. 'WHERE (#delivered_qty - #borrowed_qty) > 0 '
//. 'LIMIT 1 '
. '';
$rows = \Yii::$app->db->createCommand($SQL)
->bindParam(":item_key", $item_key)
->queryAll();
Should work. You can use datareader via query() if result set is big.
To use query builder you need to rewrite this query, which actually can be done without variables in columns.
I think this can be rewritten to:
SELECT dc.`dc_key` ,
IFNULL(SUM(dd.`qty`),0) as delivered_qty,
IFNULL(SUM(bd.`qty`),0) as borrowed_qty
LEFT JOIN `delivery_detail` dd ON dd.`item_key` = :item_key AND dd.dc_key = dc.dc_key
LEFT JOIN `borrow_detail` bd ON
bd.`item_key` = :item_key
AND bd.`voided` = 0
AND bd.`returned` = 0
AND bd.`dc_key` = dc.`dc_key`
FROM `delivery_catalog` dc
GROUP BY dc.`dc_key`
HAVING delivered_qty - borrowed_qty > 0
(#delivered_qty - #borrowed_qty) AS available_qty this can be done in PHP, and such query can be converted to query builder without problems.

How can I ignore excluded images in this query?

I have this query, it is not written by me and I'm not very good at PHP and Magento. I have this client who has several images in some products, lets say, there are 14 images for one product but half of them are excluded and they don't want to show them. So my query returns all pictures for each product. You can see the code below:
$_mediaGalleryData = $_read->fetchAll('SELECT
main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
`value`.`label`, `value`.`position`, `value`.`disabled`, `default_value`.`label` AS `label_default`,
`default_value`.`position` AS `position_default`,
`default_value`.`disabled` AS `disabled_default`
FROM `catalog_product_entity_media_gallery` AS `main`
LEFT JOIN `catalog_product_entity_media_gallery_value` AS `value`
ON main.value_id=value.value_id AND value.store_id=' . $store_id . '
LEFT JOIN `catalog_product_entity_media_gallery_value` AS `default_value`
ON main.value_id=default_value.value_id AND default_value.store_id=0
WHERE (
AND main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
AND (main.entity_id IN (' . $_read->quote($collection->getAllIds()) . '))
ORDER BY IF(value.position IS NULL, default_value.position, value.position) ASC
');
I have added value.disabled=0 like this:
WHERE (
value.disabled = 0
AND main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
AND (main.entity_id IN (' . $_read->quote($collection->getAllIds()) . '))
But it did not work either. Any ideas on how can I work that out?
EDIT: So I changed my code to following but still getting the same result;
$_mediaGalleryData = $_read->fetchAll('SELECT
main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
`value`.`label`, `value`.`position`, `value`.`disabled`, `value`.`label` AS `label_default`,
`value`.`position` AS `position_default`,
`value`.`disabled` AS `disabled_default`
FROM `catalog_product_entity_media_gallery` AS `main`
INNER JOIN `catalog_product_entity_media_gallery_value` AS `value`
ON main.value_id=value.value_id AND value.store_id=' . $store_id . '
WHERE (
value.disabled = 0
AND main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
AND (main.entity_id IN (' . $_read->quote($collection->getAllIds()) . '))
ORDER BY IF(value.position IS NULL, value.position, value.position) ASC
');

Dynamic MySQL recordset query using join syntax

I can't get the JOIN syntax correct to alter this existing MySQL query in PHP to include a join from another table. I have another table specified as DB_TABLE2 that contains columns InvmNumr and InvmDesc. The InvmNumr and InvlNumr are the exact same value in each table and I need to display InvmDesc from Table 2 in this query?
$res = mysql_query('SELECT LocId, InvlNumr, InvlQuant FROM ' . DB_TABLE1
. ' WHERE LocId = \'' . mysql_real_escape_string($cType) . '\'
AND InvlNumr = \'' . mysql_real_escape_string($br) . "'");
$markup = '';
Read up on LEFT JOIN vs INNER JOIN depending on how your data is stored in your DB_TABLE2 table.
$res = mysql_query('SELECT ' . DB_TABLE1 . '.LocId, ' . DB_TABLE1 . '.InvlNumr, ' .
DB_TABLE1 . '.InvlQuant, ' . DB_TABLE2 . '.InvmDesc
FROM ' . DB_TABLE1 . ' LEFT JOIN ' . DB_TABLE2 . ' ON ' .
DB_TABLE1 . '.InvlNumr = ' . DB_TABLE2 . '.InvmNumr
WHERE ' . DB_TABLE1 . '.LocId = \'' . mysql_real_escape_string($cType) . '\'
AND ' . DB_TABLE1 . '.InvlNumr = \'' . mysql_real_escape_string($br) . "'");
try this
SELECT db1.LocId,db1.InvlNumr,db1.InvlQuant
FROM DB_TABLE1 db1
INNER JOIN DB_TABLE2 db2 ON db1.InvlNumr = db2.InvlNumr
WHERE dbq.LocId = $cType
AND db1.InvlNumr = $br

Categories