Jump to content

MySQL Convert Currencies and Sum Group by branch


imperium2335

Recommended Posts

Hi,

 

I am trying to do some conversions on the fly with prices of products in a database.

 

I have this so far, but it is extremely slow and seems to load forever (I have a lot of data in the base):

 

<?PHP
include("currency-convert.php") ;
include("dbconnectlocal.php") ;

// GET POUNDS...
$result = mysql_query("SELECT branches.Country, users.userName, users.salesArea, enquiries.assignedToT, partbaskets.*, sum(partbaskets.total), sum(enquiries.theirShippingTotal), jobs.enquiryRef, jobs.isInvoiced
					   FROM branches, users, enquiries, partbaskets, jobs
					   WHERE partbaskets.enquiryRef = enquiries.id
					   AND enquiries.assignedToT = users.userName
					   AND users.salesArea = branches.Country
					   AND jobs.enquiryRef = enquiries.id
					   AND jobs.isInvoiced = '1'
					   AND partbaskets.currency = '£'
					   GROUP BY users.salesArea
					   ORDER BY users.salesArea ASC
					   ") ;
while($row = mysql_fetch_assoc($result)) {
	$absTotalPounds = $row['sum(partbaskets.total)'] + $row['sum(enquiries.theirShippingTotal)'] ;

	$area = $row['salesArea'] ;
	// GET DOLLARS
	$resultd = mysql_query("SELECT branches.Country, users.userName, users.salesArea, enquiries.assignedToT, partbaskets.*, sum(partbaskets.total), sum(enquiries.theirShippingTotal), jobs.enquiryRef, jobs.isInvoiced
					   FROM branches, users, enquiries, partbaskets, jobs
					   WHERE partbaskets.enquiryRef = enquiries.id
					   AND enquiries.assignedToT = users.userName
					   AND users.salesArea = '$area'
					   AND jobs.enquiryRef = enquiries.id
					   AND jobs.isInvoiced = '1'
					   AND partbaskets.currency = '$'
					   GROUP BY users.salesArea
					   ORDER BY users.salesArea ASC
					   ") ;
	while($rowd = mysql_fetch_assoc($resultd)) {
		$absTotalDollars = $rowd['sum(partbaskets.total)'] + $rowd['sum(enquiries.theirShippingTotal)'] ;
			$resultd = mysql_query("SELECT branches.Country, users.userName, users.salesArea, enquiries.assignedToT, partbaskets.*, sum(partbaskets.total), sum(enquiries.theirShippingTotal), jobs.enquiryRef, jobs.isInvoiced
					   FROM branches, users, enquiries, partbaskets, jobs
					   WHERE partbaskets.enquiryRef = enquiries.id
					   AND enquiries.assignedToT = users.userName
					   AND users.salesArea = '$area'
					   AND jobs.enquiryRef = enquiries.id
					   AND jobs.isInvoiced = '1'
					   AND partbaskets.currency = '€'
					   GROUP BY users.salesArea
					   ORDER BY users.salesArea ASC
					   ") ;
	while($rowe = mysql_fetch_assoc($resulte)) {
		$absTotaEuros = $rowe['sum(partbaskets.total)'] + $rowe['sum(enquiries.theirShippingTotal)'] ;

		//echo "<br />" . $row['salesArea'] . $absTotalPounds . " " . $absTotalDollars ;	
	}		

	}

	echo "<br />" . $row['salesArea'] . $absTotalPounds . " " . $absTotalDollars ;	
}

?>

 

What I would like is for it to return the final value in pounds against each branch, converted from the dollar and euro rate which is provided in another table. I know there must be a way for this to be more efficient!

 

I have looked into IFs in mysql but can't get my head around it.

 

I was thinking something along the lines of "IF partsbaskets.currency = '$' use dollarRate FROM currency.rates sum(partsbaskets.total * dollarRate) AS totalInDollars ELSEIF partsbaskets.currency = '€' etc etc..."

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.