MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 28th, 1999, 06:44 AM
Herring
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:

Hi,

I need to multiply two columns in a query and then add the results together
based on an id.

I`ve already made a query:

select lc.location,
fc.name,
fd.sweight*lc.lweight as score
from funcdef fd
left join
loccri lc on fd.criteria=lc.criteria
left join
locations loc on loc.ID=lc.location
where
function=1 and lc.criteria!=null

here`s the result:

location name score
5 market place 500
4 post office 3750
2 railwaystation 1000
2 railwaystation 1000
2 railwaystation 2000
2 railwaystation 2000
5 market place 1500
2 railwaystation 1500
2 railwaystation 1500
5 market place 3000

now I would need to get the cumulative results of each location. as in

loc score
5 5000
2 9000
4 3750

i`ve done this in php, but i`d like to use sql queries since the table
can grow to thousends of rows, and optimizing queries seems like a good
idea.
I don`t know how to do the second query.
is there a way to do all of this in one query, or do I need to use temporary tables?

- matti

Reply With Quote
  #2  
Old July 28th, 1999, 07:47 PM
DesertFox
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Matti,

This might spark some ideas.

In an old dos database I use, I can issue this select command:

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
SELECT COL1, SUM(COL2 * COL3) FROM TABLE1 GROUP BY COL1 WHERE ....(Whatever)
[/quote]

I haven't looked at the select syntax in the MySQL document when I posted this message. Maybe it might require some tweaking.

Hope this helps.

DesertFox

Reply With Quote
  #3  
Old July 29th, 1999, 03:13 AM
Herring
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Thanks, I got it working with this query...
someone said that doing left joins is not very efficient... I'll do some optimizing on the query later, just had to get it working first.

select
lc.location
, loc.name
, sum(fd.sweight*lc.lweight) as score
from
funcdef fd
left join loccri lc on fd.criteria=lc.criteria

left join locations loc on loc.ID=lc.location

where
fd.function=(1 or 2)
and lc.criteria!='null'
group by
lc.location
order by
score desc

- Herring

Reply With Quote
  #4  
Old July 29th, 1999, 03:16 AM
Herring
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
fd.function=(1 or 2) is a typ0 and notin the actual query : fd.function=1, sorry
- Herring

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > multiple queries and math???

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap