Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Query PHP/MYSQL question

Posted on 2011-03-16
10
264 Views
Last Modified: 2012-05-11
Hello Experts,

I was wondering if it was possible to "add" together all the numbers contained in a table based on the "company name" I ask this because the company have more than one phone per a business and need to be added up and sorted dynamicly. from highest to lowest.

An example of my table is attached

So what im trying to do is get the following

Company                TOTAL SPEND
ANNA Norris           £178
PETER jones            £100

As you can see it adds up all fields where Anna norris is the company name. as totals it.. so this would ADD both mobile number spends together. Because two people have phones in One company.

It then would need be orderd from highest to lowest?

Is This possible with a query??

if not is there any other ways you can think of??

All help is appricated.




Untitled.jpg
0
Comment
Question by:NeoAshura
10 Comments
 
LVL 3

Expert Comment

by:fabzster187
ID: 35146767
ORDER BY DESC

Should give u this
0
 
LVL 3

Expert Comment

by:fabzster187
ID: 35146773
sorry ORDER BY TOTAL SPEND DESC
0
 
LVL 17

Accepted Solution

by:
Shinesh Premrajan earned 250 total points
ID: 35146827
select sum(jan) as total from company group by company
union
select sum(feb) as total from company group by company
union
select sum(mar) as total from company group by company order by total desc

Try with this query, see if its work then you can use the same for the rest of the months
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 4

Assisted Solution

by:qasim_md
qasim_md earned 250 total points
ID: 35146866
This will give you what you want::::

select sum(total_spend),company from your_table group by company order by 1 desc

did this help ???
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35147145
Ill try these when I get back to computer just in a meeting atm. Thanks will keep u updated
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35147162
Btw total spend is NOT a table it is the total needing to be displayed by PHP once the motnhly totals have been added
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35148154
ok guys im back at computer, i tried

select sum(total_spend),company from your_table group by company order by 1 desc

but get the following error:

Unknown column 'total_spend' in 'field list'

Thats because total spend needs to be the value which is added together from the months....



I Also tried:

"select sum(Jan09) as total from Bill2009 group by company
union
select sum(Feb09) as total from Bill2009 group by company
union
select sum(Mar09) as total from Bill2009 group by company order by total desc

This returned me with a table but with blank fields...

Anymore Ideas?????
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35148169
P.S what ive got is the table as shown attached in my first opening question.

What i need from that table is the output of

Company     TOTAL SPEND
Just LTD        £200
Deck LTD       £150

THERE IS NO TOTAL SPEND COLUMN IN MY TABLE THIS MUST BE CREATE VIA ADDING UP THE JAN FEB ETC.. AND THEN SHOWING TOTAL SPEND AS THE OUTPUT USING PHP.

Is this possible??
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35155069
I solved my own problem. However some experts helped me get that answer so points will be awarded accordingly.
0
 
LVL 6

Author Closing Comment

by:NeoAshura
ID: 35155076
was not full answer, created the answer myself.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL Update query with connected table data 3 40
php56-php-mcrypt for rhel7 php56 1 54
JQuery Search Filter 2 32
Add a loading gif while php runs server side 15 35
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question