Skip to main content
Inspiring
January 27, 2009
Question

Calculate total outstanding balance

  • January 27, 2009
  • 21 replies
  • 2505 views
I have 3 tables: Jobs, Customer, Balance

The "jobs" table holds all info on the job and also holds "job_cost" which is the price.

I now need to search by customer, his cost in total for every job he has and then update the job once calculated how much he has paid and how much is remaining from the total cost for all jobs.

This is the first time trying to do calculations and I am not sure what to do.

My table and all details are in the code below:

CREATE TABLE IF NOT EXISTS `balance` (
`bal_id` int(6) NOT NULL auto_increment,
`pay_date` varchar(8) default NULL,
`amount` varchar(11) default NULL,
`outstanding` varchar(11) default NULL,
`cust_id` varchar(8) default NULL,
PRIMARY KEY (`bal_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `customer` (
`cust_id` int(8) NOT NULL auto_increment,
`cust_address` text,
`cust_tel` varchar(12) default NULL,
`cust_mobile` varchar(12) default NULL,
`cust_email` varchar(80) default NULL,
`cust_website` varchar(80) default NULL,
`cust_name` varchar(20) default NULL,
`cust_business_name` varchar(30) NOT NULL,
PRIMARY KEY (`cust_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=73 ;


++++++++++++++++
Currently I have
++++++++++++++++
mysql_select_db($database_db, $db);
$query_total_cost = "SELECT t1.*, t2.*, t3.SUM(CAST(job_cost) AS DECIMAL(6,2)) AS total_amount
FROM customer as t1, balance as t2, jobs as t3
WHERE t1.cust_business_name = t3.cust_business_name
GROUP BY t1.cust_id";

But this does not seem to work and outputs:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AS DECIMAL(6,2)) AS total_amount FROM customer as t1, balance as t2, job' at line 1
This topic has been closed for replies.

21 replies

Inspiring
January 27, 2009
The_FedEx_Guy wrote:
SELECT t1.*, t2.*, t3.SUM(CAST(job_cost) AS DECIMAL(6,2))
AS total_amount
FROM customer as t1, balance as t2, jobs as t3
WHERE t1.cust_business_name = t3.cust_business_name
GROUP BY t1.cust_id

Thats a bad way to join tables together, especially when you don't
specify which columns you want returned.

You need to read up on INNER JOIN's, cos even if that did work, it would
list all the data, from all your tables, repeated 3 times!

Dooza
--
Posting Guidelines
http://www.adobe.com/support/forums/guidelines.html
How To Ask Smart Questions
http://www.catb.org/esr/faqs/smart-questions.html