I am working on a new website for myself and the idea of it is to query the DB for two different locations, compare them and display the locations that have both sell and buy offers. This lets me know I can by X from location A and sell it at location B for profit.
Here are the tables I have
Table: marketdata
+-----------------------------------------------------+
| orderId | locationId | itemId | Price | sellorder |
------------------------------------------------------+
| 34444 | 23444 | 23 | 33 | Sell |
| 31434 | 34423 | 234 | 10.5 | Sell |
| 34415 | 34453 | 234 | 12.5 | Buy |
| 36436 | 33451 | 234 | 13.5 | Sell |
+-----------------------------------------------------+
table: locationinfo
+------------------+
| locId | locName |
+--------+---------+
| 23444 | west |
| 34423 | east |
| 34453 | north |
| 33451 | south |
+------------------+
What I need to do is get data from marketdata table if its related to 2 specific locations(IE 34423 and 34453) and I do this through 2 seprate queries. Then I need to compare the data and check to see if any of the items that I pulled are the same or not(IE order 31434 and 34415 are the same ItemId). If they are the same, then I need to see if the buy order is higher than the sell order. If it is, then I display that information. I also have a query that pulls the entire locationinfo table and as the marketdata is displayed, it does a foreach loop to find a location name that matches the marketdata location ID so I know where they are.
Here is the general look of my PHP is:
foreach (selldata as sell)
{
foreach(buydata as buy)
{
if(buydata['itemId'] == selldata['itemId'])
{
if(buydata['price'] > selldata['price']
{
foreach(locations as loc)
{
if(loc['locId'] == sell['locationId'])
{
display sell location info
break;
}
}
display rest of sell info
foreach(locations as loc]
{
if(loc['locId'] == buy['locationId'])
{
display buy location info
break;
}
display rest of buy info
}
}
}
}
}
This worked pretty well for a few searches I tried but the last one I did had over 29k results and it was rather slow processing it all and displaying it on my screen. A few times it would just time out and not display anything at all.
I know its querying my database for the 2 marketdata searches and the locationinfo search but it messes up when it tries to display it all. I took out a bunch of my code and left just one foreach loop in to have it display just one piece of data and it did that without any real issue. It had about 29k results.
Is there a more efficient way for me to compare data during the page display? Is there a way to have multiple pages for data that is currently being processed? I would like to have, say, 50 items on a page but I don't know how to do it other than to set a LIMIT on the query but that won't let me compare every item first to see which one has the most profit to display it on the top.
I hope I explained this clearly. Thanks for any help anyone can provide!
Here is my guess. SELECT only what you really need from the database:
marketdata
if its related to 2 specific locations (IE 34423 and 34453)
AND location name
that matches the marketdata location
ID
AND show the location only if buy price is higher
than the sell price of any particular item. The next query provides the information
There are 1 rows resulting from:
QUERY:
SELECT MAX(price), itemId, locationId, locName FROM marketdata
LEFT OUTER JOIN locationinfo ON marketdata.locationId = locationinfo.locId
WHERE (marketdata.locationId = 34423 OR marketdata.locationId = 34453)
AND sellorder = 'Buy'
GROUP BY itemId
MAX(price) itemId locationId locName
14.5 234 34423 east