
- 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
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)
- Related Articles
- How can I simulate a print statement in MySQL?
- How can we simulate the MySQL INTERSECT query?
- How can we simulate the MySQL MINUS query?
- How to simulate discrete uniform random variable in R?
- How MySQL LEFT JOIN can be used to simulate the MySQL MINUS query?
- How can we simulate the MySQL INTERSECT query having WHERE clause?
- How can we simulate the MySQL INTERSECT query returning multiple expressions?
- how can I declare an Object Array in Java?
- How to simulate the LIMIT MySQL clause with an Access database?
- In PHP, how can I add an object element to an array?
- How to work with array variable in MySQL?\n
- How can I remove a value from an enum in MySQL?
- How can I replace & with an ampersand in my MySQL database?
- How can I convert a Python tuple to an Array?
- How can I put a Java arrays inside an array?

Advertisements