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
.sku
ASC
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
.sku
ASC
But it did not work
SHOW CREATE TABLE stock:
CREATE TABLE
stock
(
stockid
bigint(20) NOT NULL AUTO_INCREMENT,
sku
char(25) NOT NULL,
quantity
int(5) NOT NULL,
creationdate
datetime NOT NULL,
stockfileid
smallint(5) unsigned NOT NULL,
touchdate
datetime NOT NULL,
PRIMARY KEY (stockid
)
) ENGINE=MyISAM AUTO_INCREMENT=315169 DEFAULT CHARSET=latin1
SHOW CREATE TABLE stockfile:
CREATE TABLE
stockfile
(
stockfileid
smallint(5) unsigned NOT NULL AUTO_INCREMENT,
filename
varchar(25) NOT NULL,
creationdate
datetime DEFAULT NULL,
touchdate
datetime DEFAULT NULL,
date
datetime DEFAULT NULL,
begindate
datetime DEFAULT NULL,
enddate
datetime DEFAULT NULL,
PRIMARY KEY (stockfileid
)
) ENGINE=MyISAM AUTO_INCREMENT=265 DEFAULT CHARSET=latin1