
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Generating a range of numbers in MySQL?
To generate a range of numbers in MySQL, you can use stored procedure. Firstly, we need to create a table. After that, we will create a stored procedure that generates a range of number from 10 to 1.
The following is the query to create a table −
mysql> create table GeneratingNumbersDemo −> ( −> Number int −> ); Query OK, 0 rows affected (0.55 sec)
Now you can create a stored procedure that stores a range of numbers in the table. The following is the query to create a stored procedure −
mysql> delimiter // mysql> CREATE PROCEDURE Stored_ProceduretoGenerateNumbersDemo() −> BEGIN −> DECLARE start INT DEFAULT 10; −> WHILE start > 0 DO −> INSERT GeneratingNumbersDemo VALUES (start); −> SET start = start - 1; −> END WHILE; −> END // Query OK, 0 rows affected (0.12 sec)
After that we need to call the stored procedure that fills a range of numbers in the table.
You can call the stored procedure with the help of call command. The syntax is as follows −
call yourStoredProcedureName();
Now you can call above stored procedure like this −
Calling the stored procedure −
mysql> call Stored_ProceduretoGenerateNumbersDemo(); Query OK, 1 row affected (0.85 sec)
Check whether the range of numbers are present in the table or not. The query is as follows −
mysql> select *from GeneratingNumbersDemo;
The following is the output −
+--------+ | Number | +--------+ | 10 | | 9 | | 8 | | 7 | | 6 | | 5 | | 4 | | 3 | | 2 | | 1 | +--------+ 10 rows in set (0.00 sec)
- Related Articles
- Generating range of numbers 1…n in SAP HANA
- Generating n random numbers between a range - JavaScript
- Generating random number in a range in C
- Generating desired pairs within a range using JavaScript
- Generating random numbers in Java
- Generating random numbers in C#
- Generating Random Numbers in Golang
- Generating MySQL Doxygen Documentation Content
- Prime numbers in a range - JavaScript
- Generating a unique random 10 character string using MySQL?
- Prime numbers within a range in JavaScript
- Generating the sequence of first n look and say numbers in JavaScript
- Bitwise AND of Numbers Range in C++
- Count Unary Numbers in a Range in C++
- Sum of prime numbers between a range - JavaScript
