Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:27:57+00:00 2026-06-10T12:27:57+00:00

I am trying to query top 3 customers. I have 3 tables: Customer table

  • 0

I am trying to query top 3 customers. I have 3 tables:

  • Customer table (CustomerID, Company)
  • Product table (ProductID, ProductName, Price)
  • Order table (OrderID, Date, Amount, CustomerID)
OrderID     Date        Amount      CustomerID
19      2012-08-24  20      10043
20      2012-08-24  40      10044
21      2012-08-24  60      10044
22      2012-08-24  80      10042
23      2012-08-24  90      10043
24      2012-08-24  100     10042
25      2012-08-24  50      10041

If you see this table:

  • 10042 has ordered $180 worth of products
  • 10043 has ordered $110 worth of products
  • 10044 has order $100 worth of products

How do I query this information like this:

Top 3 Customers

CustomerID  Company     Cost of Products Ordered
10042       HP      $180
10043       Acer        $110
10044       Sony        $100

Currently I have this mysql but its not displaying as how I want it. Can someone help to point out my mistake?

$query = "SELECT 
    CustomerOrder.CustomerID, CustomerOrder.Amount, 
    Customer.Company, 
    count(CustomerOrder.Amount) as total_amount
FROM 
    `CustomerOrder` 
     INNER JOIN Customer ON Customer.CustomerID = CustomerOrder.CustomerID
GROUP BY CustomerID 
ORDER BY total_amount DESC LIMIT 3";

Currently, i’m getting this:

 Top 3 Customers

CustomerID  Company         Cost of Product Ordered
10042       HP          80.00
10043       Acer            20.00
10044       Sony            40.00

I’m using this code to display:

$result = mysql_query($query);
$num=mysql_numrows($result);
echo "<table border='1'>
<tr>
<th>CustomerID</th>
<th>Company</th>
<th>Cost of Product Ordered</th>

</tr>";

while($row = mysql_fetch_array($result)) 
  {
  echo "<tr>";
  echo "<td>" . $row['CustomerID'] . "</td>";
  echo "<td>" . $row['Company'] . "</td>";
  echo "<td>" . $row['total_amount'] . "</td>";

  echo "</tr>";
  }
echo "</table>";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:27:58+00:00Added an answer on June 10, 2026 at 12:27 pm

    Use SUM instead of COUNT:

    SELECT 
        Customer.CustomerID,
        Customer.Company, 
        SUM(CustomerOrder.Amount) AS total_amount
    FROM CustomerOrder
    INNER JOIN Customer
    ON Customer.CustomerID = CustomerOrder.CustomerID
    GROUP BY Customer.CustomerID 
    ORDER BY total_amount DESC
    LIMIT 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract data from a table using... $tags = $xpath->query('//tr[@valign=top]/td/span[@style=background-color:#cccccc;font-weight:bold;]'); on
I'm trying to convert this SQL query to L2S.. SELECT TOP 5 vt.* FROM
I am trying to query some tables in my database using a simple dropdown
I am trying to query the windows desktop search API using SQL. I have
I have an issue where I am trying to order a result set by
I have a query, Im trying to select the maximum value from the summer
I am trying to add a where clause to the following query: SELECT TOP
hi i am trying to convert &#x0A into &#x0D&#x0A in select query SELECT TOP
So I have a query that is a Top Nth aggregate query, and I
Here is the query I am trying to run: SELECT TOP 5 PageComment.ID FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.