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 3500270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:42:47+00:00 2026-05-18T12:42:47+00:00

This is my first question, so I tried to be clear (hope its not

  • 0

This is my first question, so I tried to be clear (hope its not too long!)

I’m using MySQL and PHP to try and compare data in a php table originating from a MySQL table of sales data with roughly the following structure (the data represents multiple products being bought and sold at many locations).

price
quantity
type
buyflag
location

‘buyflag’ is boolean and represents whether or not the price recorded is the price the item is being bought or sold at, where buyflag=true is the price an item was bought for and buyflag=false indicates a price the product was sold at.

I’d like a query result that compares two locations by each product type when the item is bought at the first location and sold at the second location. In other words, looks something like this:

type |  quantity  |  'buyprice'  |  'sellprice'

where ‘buyprice’ is the price it was bought at location 1 and ‘sellprice’ is the price it was sold at location 2.

After some research I’ve been able to use joins and subqueries for displaying other results but am stumped in this case by how to query for the last two columns in this particular query when the data is all in a single table but seems to require two WHERE statements to dig it out.

select type, quantity, max(price) as buyprice
from marketfile
where (buyflag=true and location=102)
group by type;

will get the first 3 columns, or

select type, quantity, min(price) as sellprice
from marketfile
where (buyflag=false and location=75)
group by type;

will get the first, second and fourth columns but I can’t figure out how to get it all in the one query.

My rather poor subquery effort (failed) so far was:

select type, quantity, min(price) as buyprice,
(select max(price) from marketfile where (buyflag=false and location=75)) as sellprice
from marketfile
where (buyflag=true and location=102)
group by type;

The reason I’d like it in a single query is to then use PHP to display the query as a table so I can look down the list of hundreds of items being bought at location 1 and sold at location 2 and look at the margins on this particular route by product.

I’m tempted to work around my MySQL inadequacies by using the two separate queries as above and dumping them into a new table and then re-querying that table. This workaround doesn’t seem particularly elegant if I then want to work with many locations and therefore exponentially increasing numbers of routes (and therefore tables to work with).

Thanks in advance for any help.

  • 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-05-18T12:42:48+00:00Added an answer on May 18, 2026 at 12:42 pm

    If I understand correctly, you want to combine two rows from the table marketfile into one output row. To accomplish that, you will have to join the table with itself. You can do it by putting it twice into the FROM clause and giving it aliases:

    SELECT a.type, …
    FROM marketfile as a, marketfile as b
    WHERE (a.type = b.type) AND (a.location = 102) AND (b.location = 75) AND …
    

    Make sure to put all the necessary join conditions into the WHERE clause to select only corresponding records. Otherwise the query will take forever and return way too many results.

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

Sidebar

Related Questions

this is my first question here; hope I'll be clear enough... I've got this
First of all, sorry if this isn't an appropriate question for StackOverflow. I've tried
This is my first question, so apologies for any mistakes. I'll try and give
this is my first question here so I hope I can articulate it well
This is my first question :). Im writing a little twitter app in PHP
this is my first question here :) I know that I should not check
(my first post was not clear and confusing so I've edited the question) I
this is my first time to post a question here, I will try to
this is my first question to stackoverflow so here it goes... I use cruise
this is my first question.. so, here we go. i have a site, 100%

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.