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

  • SEARCH
  • Home
  • 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 7877463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:22:41+00:00 2026-06-03T03:22:41+00:00

I have 3 tables. Looking for a good way to find the difference in

  • 0

I have 3 tables. Looking for a good way to find the difference in field PRICE using three different tables, then displaying the top 3 largest negative differences. I want to first find the best MySQL query to use, and also find the best way to display it all in php.

MAINTABLE:

COMPANY       | MODEL    | PRICE
Main Company  | ProductA | 100.00
Main Company  | ProductB | 50.00
Main Company  | ProductC | 25.00
Main Company  | ProductD | 300.00

COMPTABLE1:

COMPANY     | MODEL    | PRICE
Competitor1 | ProductA | 100.00 //0
Competitor1 | ProductB | 55.00 //5
Competitor1 | ProductC | 50.00 //25
Competitor1 | ProductD | 200.00 //-100

COMPTABLE2:

COMPANY     | MODEL    | PRICE
Competitor2 | ProductA | 99.00 //-1
Competitor2 | ProductB | 44.00 //-6
Competitor2 | ProductC | 20.00 //-5
Competitor2 | ProductD | 100.00 //-200

So the largest negative differences in PRICE which I want displayed in my page are:

  1. Competitor2 ProductD -200 difference from Main Company ProductD
  2. Competitor1 ProductD -100 difference from Main Company ProductD
  3. Competitor2 ProductB -6 difference from Main Company ProductB

IDEA: I am not so familar with it, but I could use a ..UNION SELECT on the three tables WHERE MODEL=XXX. I could possibly loop through each one gathering the data, doing the math and spitting out the info. Only problem is, is that I don’t know how to store EACH variable as their own price for each of the tables. Also, I think it would display ALL differences unless there is a way to store each variable after doing the math, then displaying the top 3 differences.

Any ideas or suggestions to best tackle this query would be appreciated. (Note: No I cannot put them all in one table =p )

  • 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-03T03:22:43+00:00Added an answer on June 3, 2026 at 3:22 am

    Can’t help on the PHP side, but this query should get you what you need. You’ll have to do a union to get all the qualified results. This will have all columns available and pre-calculated for you to put into a simple grid listing in whatever fashion you need. Since the calculations are competitor vs main company, the PriceDifference via natural order by will have largest negative first, then go positive. So, the LIMIT command will be applied after the ordering and just send back 3 records.

    select 
          MT.Model,
          MT.Company as MainCompany,
          MT.Price as MainPrice,
          CT1.Company as Competitor,
          CT1.Price as CompPrice,
          CT1.Price - MT.Price as PriceDifference
       from
          MainTable MT
             JOIN CompTable1 CT1
                on MT.Model = CT1.Model
    UNION
    select 
          MT.Model,
          MT.Company as MainCompany,
          MT.Price as MainPrice,
          CT2.Company as Competitor,
          CT2.Price as CompPrice,
          CT2.Price - MT.Price as PriceDifference
       from
          MainTable MT
             JOIN CompTable2 CT2
                on MT.Model = CT2.Model
    order by
       PriceDifference
    limit 3
    

    Suggestion… The way you have your tables structured is really bad for the long haul. You should try to normalize you data for more optimal performance. What happens if you have 100 competitors. You have duplication all over the place. Change a model name too. Here is how I would restructure the tables… not explicit data typing, but conceptually

    COMPANY 
       CompanyID     auto-increment
       CompanyName   character
    
    PRODUCT
       ProductID     auto-increment
       ProductModel  character
    
    VendorPricing
       VPriceID      auto-increment
       CompanyID     (ID pointing to company table -- to get name when needed)
       ProductID     (ID pointing to product table -- to get model name too)
       Price         actual price for this particular company and product
    

    Then, with appropriate indexes, if you wanted to get pricing from one vendor to another, and whatever model, your query could be easier to expand in the future… something like

    select 
          VP1.CompanyID,
          C1.CompanyName as MainCompany,
          C2.CompanyName as Competitor,
          P1.ProductModel,
          VP1.Price as MainPrice,
          VP2.Price as CompetitorPrice,
          VP2.Price - VP1.Price as PriceDifference
       from
          VendorPricing VP1
    
             JOIN Company C1
                on VP1.CompanyID = C1.CompanyID
    
             JOIN Product P1
                on VP1.ProductID = P1.ProductID
    
             JOIN VendorPricing VP2
                on VP1.ProductID = VP2.ProductID
               AND NOT VP1.CompanyID = VP2.CompanyID
    
               JOIN Company C2
                  on VP2.CompanyID = C2.CompanyID
    
       where
          VP1.CompanyID = TheOneCompanyYouAreInterestedIn
       order by
          PriceDifference
       limit 3
    

    So now, if you had 2, 5, 10 or 100 competitors, the query is exactly the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an access database with many tables. I am looking for a field
Thank you for looking at my issue. Right now I have 3 tables: tblEmployee
I'm looking at building a Rails application which will have some pretty large tables
I have this query to search in two SQL tables. I am looking for
I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field
I have a table which contains many records. I'm looking for an efficient way
Good Morning, I was looking for a way to combine two integers to create
I'm using Firebird 2.1 and I'm looking for the best way to solve this
Im looking for a good way to solve my performance issues in my rails
I'm looking for a good way to maintain permissions on who can add data

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.