I need to populate a currently empty table with a hundred or so fake records to simulate logins over the past two years to test my code with.
The login table schema looks like:
CREATE TABLE `Logins` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`User_ID` int(11) NOT NULL,
`Date_Login` datetime NOT NULL,
`Location` enum('site','admin') NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
I really am new at SQL in general, so, I don’t have the slightest idea what the query should look like past
INSERT INTO `Logins` (`User_ID`,`Date_Login`,`Location`) VALUES ...
What I need is insert N entries (lets say 100) into Logins so that
User_IDdraws its values from theUserstable’sIDfieldDate_Loginshould be between 2 years ago and nowLocationshould alternate between'site'and'admin', but with'site'more heavily weighted (say 80% of the time).
Hopefully, I can glean some SQL-fu to help with similar problems in the future 😀
Thanks!
(I’m using MySQL 5.1)
Here is an SQL statement to insert a single row into the Logins table. You can run this repeatedly (e.g. in a stored procedure) to get multiple records. You have to run it multiple times to get multiple records because if you increase
LIMIT 1toLIMIT 10you will get 10 records but theUser_IDvalues will be the same for each record.Normally
ORDER BY RAND()is bad style because it is inefficient, but this isn’t a performance-sensitive task.