随机选项出题mysql
时间: 2023-11-07 16:00:41 浏览: 96
以下是随机选项出题的MySQL语句:
CREATE TABLE questions (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
question_text VARCHAR(255) NOT NULL,
option1 VARCHAR(255) NOT NULL,
option2 VARCHAR(255) NOT NULL,
option3 VARCHAR(255) NOT NULL,
option4 VARCHAR(255) NOT NULL,
correct_option INT(11) NOT NULL
);
INSERT INTO questions (question_text, option1, option2, option3, option4, correct_option)
VALUES ('What is the capital city of France?', 'Paris', 'Berlin', 'London', 'Madrid', 1),
('What is the largest planet in our solar system?', 'Jupiter', 'Saturn', 'Neptune', 'Uranus', 1),
('What is the highest mountain in the world?', 'Mount Everest', 'K2', 'Mont Blanc', 'Mount Kilimanjaro', 1),
('What is the largest ocean in the world?', 'Pacific Ocean', 'Atlantic Ocean', 'Indian Ocean', 'Arctic Ocean', 1),
('Who is the current president of the United States?', 'Joe Biden', 'Donald Trump', 'Barack Obama', 'George W. Bush', 1);
SELECT question_text,
option1,
option2,
option3,
option4,
correct_option,
CONCAT(option1, ',', option2, ',', option3, ',', option4) AS options
FROM questions
ORDER BY RAND()
LIMIT 1;
这个MySQL语句创建了一个名为“questions”的表,其中包含问题文本、四个选项和正确选项的列。然后,使用INSERT语句添加了五个问题及其选项。最后,使用SELECT语句随机选择一行,并将选项1到4组合成一个字符串返回。
阅读全文