I have 2 tables in Joomla:
Entitlements:(User_id, entitlement_amount, reference_year)
Charges:(User_id, charge_amount, reference_year)
Both must be linked to the users core table using the User_id as a foreign key.
Entitlements has entries with users not necessarily included in Charges. Charges table has entries not necessarily included in the Entitlements.
I need to link all 3 tables in such a way so that in each row, I will have the User_id(name), the SUM(entitlement_amount) and the SUM(charge_amount).
The results must be grouped by employee, reference_year.
If no SUM is calculated then to display 0.
Here is my code below.
$query->select( $this->getState(
'list.select',
'a.id,' .
'a.employee_id,' .
'ac.employee_id,' .
'a.reference_year,' .
'ac.reference_year,' .
'u.name,' .
'SUM(a.entitlement), ' .
'SUM(ac.charge)'));
$query->from($db->quoteName('#__employee_entitlement').' AS a');
$query->join('LEFT OUTER', $db->quoteName('#__users', 'u') . ' ON (' .
$db->quoteName('a.employee_id') . ' = ' . $db->quoteName('u.id') .')');
$query->join('LEFT', $db->quoteName('#__absence_charge', 'ac') . ' ON ('
. $db->quoteName('ac.employee_id') . ' = ' . $db->quoteName('u.id') .
')');
$query->group($db->quoteName('a.employee_id')) ;
$query->group($db->quoteName('a.reference_year')) ;
Thanks.
Related
I have that error
for this php code
$query = "insert into question values(" . $_SESSION['testqn'] . ",$newstd,'" . htmlspecialchars($_REQUEST['question'], ENT_QUOTES) . "','" . htmlspecialchars($_REQUEST['optiona'], ENT_QUOTES) . "','" . htmlspecialchars($_REQUEST['optionb'], ENT_QUOTES) . "','" . htmlspecialchars($_REQUEST['optionc'], ENT_QUOTES) . "','" . htmlspecialchars($_REQUEST['optiond'], ENT_QUOTES) . "','" . htmlspecialchars($_REQUEST['correctanswer'], ENT_QUOTES) . "'," . htmlspecialchars($_REQUEST['marks'], ENT_QUOTES) . ")";
"Column count doesn't match value count at row 1", this error would be shown if you have mismatch in the number of table columns and number of columns you are entering in the insert query.
Say, If you have 8 columns in table and if you are trying to insert with 7 or less columns you will get this error. I can see a mismatch in your table columns versus the insert query
Check the query and also the columns in the table, you will be able to fix this error.
I already saw other post related to this question, but they did not solve my problem. I want to automatically go to next rows and want to insert that row in the table. I am transfering sample of data from another database. I want to insert large set of rows... Right now I am getting first row repeatedly in all rows. I need to go to to next rows automoatically
Below is my sample code...
while ($row = mysql_fetch_array($query)) {
$sql = "INSERT INTO table_name (info1, info2, info3,...) "
."VALUES (" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . ")";
}
This sql Query could help you. You can insert the select datas in one Query. See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/insert-select.html
INSERT INTO table_name
(info1, info2, info3)
SELECT
source_table.info1,
source_table.info2,
source_table.info3,
FROM
source_table
WHERE
(WHERE CLAUSE)
do a separate insert for each selected rows:
while ($row = mysql_fetch_array($query)) {
$sql = "INSERT INTO table_name (incremented_field, info1, info2, info3) "
."VALUES (NULL, " . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . ")";
mysql_query($sql);
}
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.
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
I'm learning PHP and Zend Framework. The following PHP function is supposed to fill a temporary table using "INSERT INTO ... SELECT" style query. However, when I SELECT * from the newly appended table, I see that most but not all of the new records have been duplicated once. I have deleted the contents of the table each time I run this scripts. Anyone know why there would be duplicates?
public function fillTableByOfficeName($officeName) {
if ($officeName != '') {
$officePhrase = "b.oof_name ='" . $officeName . "' AND ";
} else {
$officePhrase = '';
}
$whereAddenda = $officePhrase .
"a.fil_bool_will_file_online = false AND " .
"a.fil_bool_confirmed = false AND " .
"a.fil_bool_duplicate = false AND " .
"a.fil_bool_not_found = false AND " .
"(a.fil_res_id_fk NOT IN (4,7,10) OR a.fil_res_id_fk IS NULL) AND " .
"a.fil_will_recorder_rec_id IS NULL AND " .
"d.tag_description NOT IN (
'Already a trust client',
'Not received from local office',
'Southtrust client (already centralized)')";
//"a.fil_date_of_transfer_to_will_recorder IS NULL";
$sql = "INSERT INTO adds(fil_id,REC_ID,FIRST_NAME,LAST_NAME,MIDDLE_INITIAL,SSN," .
"MAILING_ADDRESS_1,MAILING_ADDRESS_2,CITY,STATE,ZIP_CODE,PHONE_NUMBER,BIRTH_DATE," .
"ORIGINATION_OFFICE,FILE_LOCATION,WILL_DATE,LAST_CODICIL_DATE,TRUST_DATE,REV_TRUST,POA_DATE) " .
"SELECT a.fil_id_pk, " .
"a.fil_will_recorder_rec_id, " .
"a.fil_first_name, " .
"a.fil_last_name, " .
"a.fil_middle_name, " .
"a.fil_ssn, " .
"a.fil_mailing_address_1, " .
"a.fil_mailing_address_2, " .
"a.fil_city_address, " .
"a.fil_state_address, " .
"a.fil_zip_code_fk, " .
"a.fil_phone_number, " .
"a.fil_date_of_birth, " .
"b.oof_name, " .
"a.fil_box_id_fk, " .
"a.fil_date_of_will, " .
"a.fil_date_of_last_codicil, " .
"a.fil_date_of_trust, " .
"a.fil_notes, " .
"a.fil_date_of_poa " .
"FROM files a, origination_offices b, nn_files_tags c, tags d " .
"WHERE " .
"a.fil_oof_id_fk = b.oof_id_pk AND " .
"a.fil_id_pk = c.fil_id_fk AND " .
"d.tag_id_pk = c.tag_id_fk AND " .
$whereAddenda;
$this->getAdapter()->query($sql);
return $this;
}
The way you are joining the table will give you the cartesian product of the rows from the tables (all pairs of matching rows are returned).
With no specific knowledge of the domain, I would guess at the tags table - if you've got multiple tags for a particular file, you will get multiple copies of the file in your result set (one per each matched tag).
As you're not using tags fields in the result set, just the where clause, the solution would be to get rid of tags / nn_files_tags from the main query, and in your where clause, use NOT EXISTS to check for matching rows in the tags table, something like:
AND NOT EXISTS (SELECT tag_id_pk FROM tags WHERE tags.tag_id_pk ...
You are using C for a many to many relationship. For example, if you have invoices between companies and customers and you select from join of them, you will get as many rows as you have invoices. From that, if you only select the company name and costumer name, you will have many duplicates because the same pair has produced many invoices.
This is the same issue you have here.
As asc99c said, you could use an inner select to make your WHERE clause without joining on that relationship or you could use the DISTINCT key word (which effectively is a group by on everything in your SELECT clause). I would think the INNER SELECT solution more efficient (yet I could be totally wrong about that), but the DISTINCT way is 8 key press away...