Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How can I simulate an array variable in MySQL?
Instead of simulating an array variable, use temporary table in MySQL. The syntax is as follows −
create temporary table if not exists yourTemporaryTableName select yourColumnName1,yourColumnName2,......N from yourTableName where condition
To understand the above syntax, let us first create a table. The query to create a table is as follows −
mysql> create table SimulateArrayDemo -> ( -> Id int, -> FirstName varchar(100), -> LastName varchar(100 -> ) -> ); Query OK, 0 rows affected (1.25 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into SimulateArrayDemo values(1,'Sam','Taylor'); Query OK, 1 row affected (0.10 sec) mysql> insert into SimulateArrayDemo values(2,'Carol','Taylor'); Query OK, 1 row affected (0.18 sec) mysql> insert into SimulateArrayDemo values(3,'Bob','Smith'); Query OK, 1 row affected (0.09 sec) mysql> insert into SimulateArrayDemo values(4,'David','Taylor'); Query OK, 1 row affected (0.16 sec) mysql> insert into SimulateArrayDemo values(5,'John','Smith'); Query OK, 1 row affected (0.11 sec) mysql> insert into SimulateArrayDemo values(6,'Mike','Taylor'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from SimulateArrayDemo;
Output
+------+-----------+----------+ | Id | FirstName | LastName | +------+-----------+----------+ | 1 | Sam | Taylor | | 2 | Carol | Taylor | | 3 | Bob | Smith | | 4 | David | Taylor | | 5 | John | Smith | | 6 | Mike | Taylor | +------+-----------+----------+ 6 rows in set (0.00 sec)
The following is the query that simulates an array variable using temporary table −
mysql> create temporary table if not exists SimulationOfArray -> select Id,FirstName from SimulateArrayDemo where LastName='Taylor'; Query OK, 4 rows affected (0.10 sec) Records: 4 Duplicates: 0 Warnings: 0
Display the records from temporary table. The query is as follows −
mysql> select *from SimulationOfArray;
Output
+------+-----------+ | Id | FirstName | +------+-----------+ | 1 | Sam | | 2 | Carol | | 4 | David | | 6 | Mike | +------+-----------+ 4 rows in set (0.00 sec)
Advertisements
