MySQL (and PHP) single query, single table, multiple qualifiers
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 d开发者_如何学JAVAata 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.
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.
精彩评论