After days of failing I hope that someone more skilled can help me with a solution.
I have two tables, one containing stocks and the other stock values. Please, you do not have to comment on field types etc as this is not a production development, I am only trying to get a grasp on join and mysql alias.
-- Stocks table:
DROP TABLE IF EXISTS `stocks`;
CREATE TABLE `stocks` (
`stock_id` int(11) NOT NULL AUTO_INCREMENT,
`stock_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`stock_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- Sample records:
INSERT INTO `stocks` VALUES ('1', 'HighTech');
INSERT INTO `stocks` VALUES ('2', 'NanoTech');
INSERT INTO `stocks` VALUES ('3', 'DotCom');
INSERT INTO `stocks` VALUES ('4', 'NewBiz');
-- Values table:
DROP TABLE IF EXISTS `vals`; CREATE TABLE `values` ( `vals_id` int(11) NOT NULL AUTO_INCREMENT, `stock_id` varchar(255) DEFAULT NULL, `stock_value` int(11) DEFAULT NULL, PRIMARY KEY (`vals_id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
-- Sample records:
INSERT INTO `vals` VALUES ('1', '1', '50');
INSERT INTO `vals` VALUES ('2', '1', '700');
INSERT INTO `vals` VALUES ('3', '1', '540');
INSERT INTO `vals` VALUES ('4', '3', '15');
INSERT INTO `vals` VALUES ('5', '3', '44');
INSERT INTO `vals` VALUES ('6', '1', '60');
INSERT INTO `vals` VALUES ('7', '2', '10');
INSERT INTO `vals` VALUES ('8', '3', '53');
There could be 100s of stocks and 1000s of value records.
What I want to do is to print each stock together with a single (latest) stock value. For stock number 3 I want to echo "DotCom" and the latest value "53", none of the others values.