jeudi 7 juillet 2016

Selecting data MYSQL

In my MySQL database there are 2 tables:

Table-1:
COMMODITY_COLUMN
CommodityType1
CommodityType2
..............
CommodityType20


Table-2
RECORD_ID    COMMODITY_COLUMN       VALUE   DATE
1             CommodityType6         156     15/08/96
2             CommodityType13        56      25/12/16   
3             CommodityType5         756     15/02/05
.             CommodityType4         584
.             CommodityType13        48
.             CommodityType2         78
.             CommodityType2         8
.             CommodityType6         55
.             CommodityType14        7
.             CommodityType4         88
.             ..........................
128           CommodityType5         756     19/04/72
129           CommodityType2         999     25/04/85

Table-1 column "COMMODITY_COLUMN" used as foreign key in the Table-2. What I am trying to do is to collect LAST values of each 20 CommodityTypes from the Table-2.

I have tryied different MYSQL expressions such as:

SELECT TABLE-2.VALUE FROM TABLE-2 WHERE COMMODITY_COLUMN IN (SELECT COMMODITY_COLUMN FROM TABLE-1) ORDER BY RECORD_ID DESC LIMIT 1

But I am not succeeded. I can create 20 diffetent expression for each 20 commodity codes but I do not think it is good way of programming.

Could anybody suggest please a solution?

Aucun commentaire:

Enregistrer un commentaire