Monday, 30 September 2013

mySql: getting latest transaction made for a row from another table

mySql: getting latest transaction made for a row from another table

I'm trying to query for the last transaction made for each item on
tbl_invty that doesn't have the transaction type "Idle" from table
tbl_trans. The multiplicity of transactions is confusing me on getting my
query right and all I was able to do so far was joining the two tables
below on tbl_invty.code=tbl_trans.code. How do I go about this so I could
output only rows 2 and 3 from tbl_invty joined with rows 5 and 8 from
tbl_trans based on the column code?
tbl_invty
+------+-------------+
| CODE | DESCRIPTION |
+------+-------------+
| 1 | abc |
| 2 | bbb |
| 3 | cdf |
+------+-------------+
tbl_trans
+----------+------+--------+------------+
| TRANS_ID | CODE | TYPE | TRANS_DATE |
+----------+------+--------+------------+
| 1 | 1 | NEW | 2012-09-29 |
| 2 | 1 | UPDATE | 2012-09-30 |
| 3 | 1 | IDLE | 2012-09-30 |
| 4 | 2 | NEW | 2012-09-29 |
| 5 | 2 | UPDATE | 2012-09-30 |
| 6 | 3 | NEW | 2012-09-29 |
| 7 | 3 | UPDATE | 2012-09-30 |
| 8 | 3 | UPDATE | 2012-09-30 |
+----------+------+--------+------------+

No comments:

Post a Comment