I have two tables "marketplace" and "stats". I have a trigger on Inserts to marketplace that takes this new data and also inserts it into stats like so:
CREATE TRIGGER `marketplace_to_stats`
BEFORE INSERT ON `marketplace`
FOR EACH ROW
INSERT INTO
stats (stats.studentID, stats.listingID, stats.listingName, stats.date, stats.percent, stats.listingViews, stats.listingWatching, stats.listingSales)
VALUES(NEW.studentID, NEW.listingID, NEW.listingName, curdate(), 0, 0, 0, 0)
However, the listingID in the marketplace table is auto incremented, and the insert statements i use leave that column out. therefore, my trigger isn't gathering the auto incremented value from the marketplace table and instead inserts 0 each time.
How can I change this trigger statement to gather and insert to the stats table, the auto incremented value generated on the marketplace table?
Tables:
--
-- Table structure for table `marketplace`
--
CREATE TABLE `marketplace` (
`listingID` int(11) NOT NULL,
`studentID` int(11) NOT NULL,
`listingName` tinytext NOT NULL,
`fileName` tinytext NOT NULL,
`listingBio` varchar(1500) NOT NULL,
`listingPrice` decimal(5,2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Table structure for table `stats`
--
CREATE TABLE `stats` (
`studentID` int(11) NOT NULL,
`listingID` int(11) NOT NULL,
`date` date NOT NULL,
`listingName` tinytext NOT NULL,
`percent` decimal(5,2) NOT NULL,
`listingViews` int(11) NOT NULL,
`listingWatching` int(11) NOT NULL,
`listingSales` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
If you want auto-generated columns, then use an after insert
trigger:
CREATE TRIGGER `marketplace_to_stats`
AFTER INSERT ON `marketplace`
FOR EACH ROW
BEGIN
INSERT INTO stats (studentID, listingID, listingName, date, percent, listingViews, listingWatching, listingSales)
VALUES (NEW.studentID, NEW.listingID, NEW.listingName, curdate(), 0, 0, 0, 0);
END;
Although MySQL allows the columns in the insert
to be qualified, I find that is unnecessary (and incompatible with other databases).