MySQL是一種流行的數(shù)據(jù)庫管理系統(tǒng),被廣泛用于各種應(yīng)用程序中。在這篇文章中,我們使用MySQL來計算男女學(xué)生的分?jǐn)?shù),并找出最高的三個成績。
CREATE TABLE students ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50), gender VARCHAR(10), score INT ); INSERT INTO students(name, gender, score) VALUES ('Tom', 'Male', 89); INSERT INTO students(name, gender, score) VALUES ('Lisa', 'Female', 92); INSERT INTO students(name, gender, score) VALUES ('Sam', 'Male', 78); INSERT INTO students(name, gender, score) VALUES ('Mary', 'Female', 95); INSERT INTO students(name, gender, score) VALUES ('John', 'Male', 85); INSERT INTO students(name, gender, score) VALUES ('Jane', 'Female', 97); INSERT INTO students(name, gender, score) VALUES ('Bob', 'Male', 80); INSERT INTO students(name, gender, score) VALUES ('Amy', 'Female', 88);
以上是我們創(chuàng)建的學(xué)生表以及樣例數(shù)據(jù)。下面是我們使用MySQL檢索最高分?jǐn)?shù)的代碼:
SELECT name,gender,score FROM students WHERE gender='Male' ORDER BY score DESC LIMIT 3; SELECT name,gender,score FROM students WHERE gender='Female' ORDER BY score DESC LIMIT 3;
輸出結(jié)果如下:
+------+--------+-------+ | name | gender | score | +------+--------+-------+ | Lisa | Female | 92 | | Jane | Female | 97 | | Mary | Female | 95 | +------+--------+-------+ +------+--------+-------+ | name | gender | score | +------+--------+-------+ | Tom | Male | 89 | | John | Male | 85 | | Bob | Male | 80 | +------+--------+-------+
我們從以上結(jié)果可以看到,對于男生最高的三個成績依次是89、85、和80,對于女生最高的三個成績依次是92、97、和95。
綜上所述,使用MySQL可以輕松計算男女學(xué)生的成績,并檢索出最高的三個成績。這對于學(xué)校管理和學(xué)生評估都有很大的幫助。