在MySQL中,我們可以使用計算函數(shù)和格式化函數(shù)來顯示百分比例。
SELECT COUNT(*) AS total, COUNT(CASE WHEN score>60 THEN 1 END) AS pass, COUNT(CASE WHEN score<=60 THEN 1 END) AS fail, CONCAT(FORMAT(COUNT(CASE WHEN score>60 THEN 1 END)/COUNT(*), 2)*100, '%') AS pass_rate, CONCAT(FORMAT(COUNT(CASE WHEN score<=60 THEN 1 END)/COUNT(*), 2)*100, '%') AS fail_rate FROM student;
上述代碼中,我們首先使用COUNT函數(shù)統(tǒng)計總人數(shù)、及格人數(shù)和不及格人數(shù)。然后利用CASE WHEN語句進行條件判斷,分別統(tǒng)計及格人數(shù)和不及格人數(shù)。
接著,利用FORMAT函數(shù)將及格人數(shù)和不及格人數(shù)除以總人數(shù),并將結果格式化為百分比形式。最后利用CONCAT函數(shù)將結果和百分號連接起來。
運行以上代碼,即可在MySQL中顯示該班級的及格率和不及格率。