MIN

Sintassi

MIN([DISTINCT] espr)

Spiegazione

Restituisce il valore minimo di espr. MIN() può accettare un argomento stringa; in tal caso, restituisce il massimo argomento stringa. La parola chiave DISTINCT serve a trovare il massimo valore unico di espr, tuttavia produce lo stesso risultato che si ottiene omettendo DISTINCT.

Si noti che i campi SET e ENUM vengono comparati in base al loro valore stringa, non alla posizione nell'insieme, perciò il valore minore letto da MIN() potrebbe essere diverso da quello letto da ORDER BY ASC.

MIN() restituisce NULL se non vi sono righe.

Esempi

CREATE TABLE student (name CHAR(10), test CHAR(10), score TINYINT); 

INSERT INTO student VALUES 
  ('Chun', 'SQL', 75), ('Chun', 'Tuning', 73), 
  ('Esben', 'SQL', 43), ('Esben', 'Tuning', 31), 
  ('Kaolin', 'SQL', 56), ('Kaolin', 'Tuning', 88), 
  ('Tatiana', 'SQL', 87), ('Tatiana', 'Tuning', 83);

SELECT name, MIN(score) FROM student GROUP BY name;
+---------+------------+
| name    | MIN(score) |
+---------+------------+
| Chun    |         73 |
| Esben   |         31 |
| Kaolin  |         56 |
| Tatiana |         83 |
+---------+------------+

MIN() con una stringa:

SELECT MIN(name) FROM student;
+-----------+
| MIN(name) |
+-----------+
| Chun      |
+-----------+

Bisogna evitare il seguente errore comune, perché un raggruppamento non corretto porta a dati non dorretti:

SELECT name,test,MIN(score) FROM student;
+------+------+------------+
| name | test | MIN(score) |
+------+------+------------+
| Chun | SQL  |         31 |
+------+------+------------+

Differenza tra ORDER BY ASC e MIN():

CREATE TABLE student2(name CHAR(10),grade ENUM('b','c','a'));

INSERT INTO student2 VALUES('Chun','b'),('Esben','c'),('Kaolin','a');

SELECT MIN(grade) FROM student2;
+------------+
| MIN(grade) |
+------------+
| a          |
+------------+

SELECT grade FROM student2 ORDER BY grade ASC LIMIT 1;
+-------+
| grade |
+-------+
| b     |
+-------+

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.