SQL/MySQL/Math/Max Min

Материал из SQL эксперт
Версия от 10:16, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Retrieving Maximum and Minimum Values

/*
mysql> Select * from StudentExam;
+-----------+------+------------+
| StudentID | Mark | Comments   |
+-----------+------+------------+
|        10 |   76 | Java       |
|        10 |   65 | C#         |
|        10 |   79 | JavaScript |
+-----------+------+------------+
3 rows in set (0.04 sec)
mysql> /* Real command */
mysql> SELECT MAX(Mark) AS TopMark, MIN(Mark) AS BottomMark
    -> FROM StudentExam
    -> WHERE StudentID = 10;
+---------+------------+
| TopMark | BottomMark |
+---------+------------+
|      79 |         65 |
+---------+------------+
1 row in set (0.00 sec)

*/
/* Create table */
Drop TABLE StudentExam;
CREATE TABLE StudentExam (
   StudentID  INT NOT NULL,
   Mark       INT,
   Comments   VARCHAR(255)
   
)TYPE = InnoDB;
/* Insert data */
INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (10,76,"Java");
INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (10,65,"C#");
INSERT INTO StudentExam (StudentID,Mark,Comments) VALUES (10,79,"JavaScript");
Select * from StudentExam;

/* Real command */
SELECT MAX(Mark) AS TopMark, MIN(Mark) AS BottomMark
FROM StudentExam
WHERE StudentID = 10;