English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Pour l'exemple de sélection récursive, regardons un exemple. D'abord, nous allons créer une table. La commande CREATE est utilisée pour créer une table.
mysql> CREATE table tblSelectDemo - > ( - > id int, - > nom varchar(100) - > );
Maintenant, nous allons insérer des enregistrements dans la table 'tblSelectDemo'.
mysql> insert into tblSelectDemo values(1,'John'); mysql> insert into tblSelectDemo values(2,'Carol'); mysql> insert into tblSelectDemo values(3,'Smith'); mysql> insert into tblSelectDemo values(4,'David'); mysql> insert into tblSelectDemo values(5,'Bob');
Afficher tous les enregistrements.
mysql> SELECT *from tblSelectDemo;
Ceci est la sortie.
+------+-------+ | id | nom | +------+-------+ | 1 | John | | 2 | Carol | | 3 | Smith | | 4 | David | | 5 | Bob | +------+-------+ 6 rows in set (0.00 sec)
Voici la syntaxe de SELECT récursif.
mysql> SELECT var1.id as id, @sessionName := var1.Nom as NomdeStudent - > from (select * from tblSelectDemo order by id desc) var1 - > join - > select @sessionName := 4)tmp - > where var1.id = @sessionName;
Ceci est la sortie.
+------+----------------+ | id | Nom de l'étudiant | +------+----------------+ | 4 | David | +------+----------------+ 1 row in set (0.00 sec)