Fonction SQL FIRST()Function
Fonction FIRST()
La fonction FIRST() renvoie la valeur du premier enregistrement dans la colonne spécifiée.
Syntaxe de SQL FIRST()
SELECT FIRST(column_name) FROM table_name;
Remarque :Seul MS Access prend en charge la fonction FIRST().
Solution de contournement pour SQL FIRST() dans SQL Server, MySQL et Oracle
Syntaxe SQL Server
SELECT TOP 1 column_name
FROM table_name
ORDER BY column_name ASC;
ORDER BY column_name ASC;
Exemple
SELECT TOP 1 name FROM Websites
ORDER BY id ASC;
ORDER BY id ASC;
Syntaxe MySQL
SELECT column_name FROM table_name
ORDER BY column_name ASC
LIMIT 1;
ORDER BY column_name ASC
LIMIT 1;
Exemple
SELECT name FROM Websites
ORDER BY id ASC
LIMIT 1;
ORDER BY id ASC
LIMIT 1;
Syntaxe Oracle
SELECT column_name FROM table_name
ORDER BY column_name ASC
WHERE ROWNUM <=1;
ORDER BY column_name ASC
WHERE ROWNUM <=1;
Exemple
SELECT name FROM Websites
ORDER BY id ASC
WHERE ROWNUM <=1;
ORDER BY id ASC
WHERE ROWNUM <=1;
Base de données de démonstration
Dans ce tutoriel, nous utiliserons la base de données d'exemple EXAMPLE.
Voici les données sélectionnées dans la table "Websites" :
+----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 3 | Example | http://www.example.com/ | 4689 | CN | | 4 | 微博 | http://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | | 6 | 百度 | https://www.baidu.com/ | 4 | CN | | 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND | +----+---------------+---------------------------+-------+---------+
Exemple SQL FIRST()
L'instruction SQL suivante sélectionne la valeur du premier enregistrement dans la colonne "name" de la table "Websites" :
Exemple
SELECT name AS FirstSite FROM Websites LIMIT 1;
L'exécution du SQL ci-dessus produit le résultat suivant :