tricomp
Založený: 20.04.2012
Príspevky: 6
Zaslal: 2012-04-20 18:46
Návrat hore
Odpovedať s citátom
|
Hello everyone,
I need some help here.
I have 2 tables: customers and invoices. Each customer in the "customers" table has one or more invoice records in the "invoices" table.Each invoice in the "invoice" table has an "amount" field.
I want to have the customers displayed in PME and to include a virtual column that shows the total amount from the invoices table for each customer.
This will be like executing the following for each row containing customer information:
| Kód: |
| SELECT SUM(amount) FROM invoices WHERE customer_id = {id_from_customers_table} |
I hope I was able to make it clear.
I'd appreciate any help I get on this.
Thanks in advance.
Feyisayo
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2012-04-21 13:17
Návrat hore
Odpovedať s citátom
|
No provision exists for totaling (summing) columns:
http://opensource.platon.sk/forum/projects/viewtopic.php?t=994127&highlight=column+total
If you can use phpMyAdmin's SQL window to produce the desired report, then you can save that report as a non-editable VIEW, and phpMyEdit will be able to read the VIEW. You might want to use the Search feature to look for alternative solutions or work-around.
|
tricomp
Založený: 20.04.2012
Príspevky: 6
Zaslal: 2012-04-21 19:58
Návrat hore
Odpovedať s citátom
|
Thanks for the reply Doug.
I did not know PME could load saved views from PHPMyAdmin.
I would love to give that a try but I don't know how. Can you give me quick information on how to. Or do the know where I can find the documentation to do so?
Feyisayo
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2012-04-21 23:33
Návrat hore
Odpovedať s citátom
|
You web host usually provides phpMyAdmin in the website control panel. If not, then your web host is second rate. At the demo
http://demo.phpmyadmin.net/master-config/
click on the SQL window atop the screen. Enter a query and hit Go.
I recommend Hostgator as a web host and have 100+ client domains hosted there. The cPanel has a lot of good tools.
http://secure.hostgator.com/~affiliat/cgi-bin/affiliates/clickthru.cgi?id=denver27
|
tricomp
Založený: 20.04.2012
Príspevky: 6
Zaslal: 2012-04-22 20:35
Návrat hore
Odpovedať s citátom
|
Thanks for that response.
I actually meant that how does one use the exported view in PME?
Feyisayo
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2012-04-23 01:19
Návrat hore
Odpovedať s citátom
|
The same way you'd use it with any table. Create a form. Disable Add, Change, Copy, and Delete since those actions won't work with a view and might damage something.
|
tricomp
Založený: 20.04.2012
Príspevky: 6
Zaslal: 2012-04-23 11:23
Návrat hore
Odpovedať s citátom
|
Sorry Doug if this seems "duh" but I didn't understand your last reply.
I figured out the SQL statement that does displays the data I want. Here it is:
| Kód: |
SELECT customers.*, total
FROM customers
LEFT JOIN (
SELECT customer_id, sum( amount_payable ) as total FROM invoices GROUP BY customer_id
) AS tb ON tb.customer_id = customers.customer_id
|
The above code works in PHPMyAdmin.
However, the challenge is to get PME to display the same data as this SQL statement displays.
|
tricomp
Založený: 20.04.2012
Príspevky: 6
Zaslal: 2012-04-23 13:16
Návrat hore
Odpovedať s citátom
|
Hey Doug,
I figured it out!!!
There were 2 tricks involved:
1) Instead of using a normal table as the join table I used a SELECT statement that calculates the sub-totals for distinct customer IDs.
2) I removed the "`" character that PME uses to delimit all field names. The "`" character makes everything in the SELECT statement a literal thus preventing the SUM function from working and causing PME to return a "1033 incorrect table" error.
Below are 2 code snippets illustrating the points above:
Point 1:
| Kód: |
$main_table = 'customers';
$join_table = '(SELECT customer_id, SUM(amount_payable) as total FROM invoices GROUP BY customer_id)';
$opts['fdd']['total'] = array(
'name' => 'Total Receipts',
'input' => 'VR',
'sql' => 'null',
'select' => 'T',
'options' => 'VACPDL',
'maxlen' => 81,
'sort' => true,
'values' => array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'customer_id', // the key field in the joined table
'join' => '$main_table.customer_id = $join_table.customer_id',
'description' => 'total' // the desired column in the joined table
),
'number_format' => array(2, '.', ',')
);
// Now important call to phpMyEdit
require_once 'phpMyEdit.class.php';
new phpMyEdit($opts);
|
Point 2:
| Kód: |
// This edit is in the function get_SQL_join_clause() of the
// phpMyEdit.class.php file. Line 637
$join_clause .= " LEFT OUTER JOIN ". str_replace('`', '' , $dbp.$table) ." AS $join_table ON (";
|
I hope someone else finds this useful.
Thanks for your help Doug.
Feyisayo
|
doug
Založený: 10.02.2003
Príspevky: 1013
Bydlisko: Denver, Colorado (USA)
Zaslal: 2012-04-23 17:39
Návrat hore
Odpovedať s citátom
|
Nice work! Having removed the backtick character, make sure none of the columns are named using Reserved Words, which would produce unexpected results.
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
|
tricomp
Založený: 20.04.2012
Príspevky: 6
Zaslal: 2012-04-24 07:19
Návrat hore
Odpovedať s citátom
|
So far none of the fields are reserved words. I'll take of it though if I have to add more fields.
Thanks again Doug.
|
|
Odoslať novú tému
Odpovedať na tému
|