0

I am a math teacher. I am currently developing a web app to display questions.

I am currently writing the questions myself and pulling from a database. I have a database with several columns like question, answer, author etc.

What I would like is to generate some data automatically. I would like a load of questions that look like (rand 2 digit number) + (2 digit number) in the question column and the answer in the answer column.

Can this be done with a MySQL command or would I need to write a script in something like PHP to do this?

Cheers.

Edit: I tried this but I think it might be nonsense

SELECT @num1 := Rand()*100 , @num2 := Rand()*100;
INSERT INTO `testDB`(TopicID, TopicName, SubtopicID, SubtopicName, Question, Answer, Difficulty, Author, Projectable) VALUES (1,Addition,5,Addition with two digits,CONCAT(@num1, " + ", @num2, " = "),@num1 + @num2,1,Richard Tock,Yes);
Angel Politis
  • 10,955
  • 14
  • 48
  • 66

2 Answers2

0

You can certainly do this with an SQL statement (see, for instance this question)

INSERT INTO table (num_1, num_2) VALUES(RAND(), RAND());

However, for more meaningful or complex data, I always code a PHP script. For instance, I just finished one where I created some arrays with the 50 most common forename, surname and largest companies, and then generated random people and email address for them.

Angel Politis
  • 10,955
  • 14
  • 48
  • 66
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • Yeah. I think the issue is actually getting the random numbers then creating a string like “rand1 + rand2” and then doing some math and creating an answer, too. Maybe best with php. – Richard Tock Feb 01 '18 at 11:45
0

I solved my own problem

SELECT @num1 := FLOOR(Rand()*100) , @num2 := FLOOR(Rand()*100);
INSERT INTO `QuestionDB`(TopicID, TopicName, SubtopicID, SubtopicName, Question, Answer, Difficulty, Author, Projectable) VALUES (1,"Addition",5,"Addition with two digits",CONCAT(@num1, "  + ", @num2, " = "),@num1 + @num2,1,"Richard Tock","Yes");

Seemed to do the trick