I have stock quantity information in my database.
1 table, "stock", holds the productid (sku) along with the quantity and the filename from where it came.
The other table, "stockfile", contains all the processed filenames along with dates.
Now I need to get all the products with their latest stock quantity values.
This gives me ALL the products multiple times with all their stock quantity (resulting in 300.000 records)
SELECT stock.stockid, stock.sku, stock.quantity, stockfile.filename, stockfile.date
FROM stock
INNER JOIN stockfile ON stock.stockfileid = stockfile.stockfileid
ORDER BYstock.skuASC
I already tried this:
SELECT * FROM stock
INNER JOIN stockfile ON stock.stockfileid = stockfile.stockfileid
GROUP BY sku
HAVING stockfile.date = MAX( stockfile.date )
ORDER BYstock.skuASC
But it did not work
SHOW CREATE TABLE stock:
CREATE TABLE
stock(
stockidbigint(20) NOT NULL AUTO_INCREMENT,
skuchar(25) NOT NULL,
quantityint(5) NOT NULL,
creationdatedatetime NOT NULL,
stockfileidsmallint(5) unsigned NOT NULL,
touchdatedatetime NOT NULL,
PRIMARY KEY (stockid)
) ENGINE=MyISAM AUTO_INCREMENT=315169 DEFAULT CHARSET=latin1
SHOW CREATE TABLE stockfile:
CREATE TABLE
stockfile(
stockfileidsmallint(5) unsigned NOT NULL AUTO_INCREMENT,
filenamevarchar(25) NOT NULL,
creationdatedatetime DEFAULT NULL,
touchdatedatetime DEFAULT NULL,
datedatetime DEFAULT NULL,
begindatedatetime DEFAULT NULL,
enddatedatetime DEFAULT NULL,
PRIMARY KEY (stockfileid)
) ENGINE=MyISAM AUTO_INCREMENT=265 DEFAULT CHARSET=latin1