Hi to all,
This is a beginner question and all help and advice would be greatly appreciated.
I am trying to join 2 tables and is having difficulty on trying to show a list of items in inventory.
Every item has a record in the "inventory" table and accompanying details about that item, which is stored in the "inventory_details" table. Every now and then, I update the details of a certain item and inserts that update detail on the "inventory details" table.
In short the "inventory" table acts as the "master" table and the "inventory_details" table acts as the "details" table.
The problem is that I want to view the latest info about a certain item and along with that info is the latest detail of that item. I only know that this could be done by a JOIN and tried doing one but the result has multiplied.
SAMPLE DATA:
Code:
"inventory" table:
inv_id item_name item_property_code item_serial_number
-----------------------------------------------------------
23 Item P prop-P serial-P
32 Item A prop-A serial-A
94 Item X prop-X serial-X
"inventory_details" table:
inv_details_id inv_id remarks school_year
----------------------------------------------------------
1 32 good 2002
2 94 good 2001
3 32 good 2003
4 94 good 2002
5 23 transferred 2005
6 94 lost 2003
7 32 good 2004
8 32 broken 2007
When I try to join the 2 tables, I get 8 records all in all. I was hoping to see only 3 records because there are 3 records on the "inventory" table. I just wanted to include the latest record which holds the "school_year" and "remarks" field of those 3 items by basing on the "school_year" field.
What I did was include a MAX(school_year) on my query and used a GROUP clause, by grouping it on the "inv_id" field.
My query:
Code:
SELECT inventory.inv_id, item_name, item_property_code, item_serial_number, remarks, MAX(school_year) FROM
inventory INNER JOIN inventory_details ON inventory.inv_id = inventory_details.inv_id
GROUP BY inventory.inv_id
ORDER BY inventory.inv_id;
RESULTS: (item_property_code and item_serial_number were not omitted)
Code:
inv_id item_name remarks max(school_year)
-----------------------------------------------
23 item P transferred 2005
32 item A good 2007
94 item X good 2003
The "school_year" field display is correct but the "remarks" does not match the exact record in the "inventory_details" table. "item A" remark's must be "broken" instead of "good", and "item X" must be "lost" instead of "good".
If this is the case, should I retrieve the "inv_details_id" (which is the PK of table "inventory_details") , so that I can get the correct "remarks" field and then include it in my JOIN query? A sub-query would be appropriated for this problem?
many thanks...
by the way, I am using MySQL 5.0 as my DBMS and here is the table definition:
1.) Table "inventory"
Fields:
inv_id - Primary Key
item_name - varchar
item_property_code - varchar
item_serial_number - varchar
2.) Table "inventory_details"
Fields:
inv_details_id - Primary Key
inv_id - Foreign Key of "inventory" table
remarks - varchar
school_year - year[/HTML]