Introduction to MySQL WHERE IN Array
MySQL supports different data types that are implemented for inserting different types of data values to store in the database. Similarly, an array is a data type that is responsible to store column values. An array is type of data structure defined in MySQL. MySQL provides WHERE IN clause that is useful to apply in the array variable to produce the query set from specific table in the database. WHERE IN clause supports to fetch data values from an array of parameters provided in the query statement in MySQL. Here, the WHERE works as conditional expression similar to other programming languages used that is essential to compare between the given data values of a specific field value that is available in the MySQL database. In an array, the WHERE IN is used where IN() function is implemented to find out a particular value within the range of column values provided as parameters.
Following is an elementary syntax structure to code for MySQL WHERE IN Array command in MySQL server to fetch information using array values and WHERE IN clause:
SELECT ColumnName1, ColumnName2, …., ColumnNameNFROM TableNameWHERE ColumnName1 IN(ColumnName1_Value1, ColumnName1_Value2, ColumnName1_Value3);
Here, in the above syntax structure we have defined the following terms:
- ColumnName1 denotes the name of the column present in the table.
- TableName is the specified table holding array values in the database to use MySQL WHERE IN Array.
- After WHERE clause, specific column name is defined from which the data is retrieved using SELECT statement and from which the IN() function includes the range of values as parameters.
How WHERE IN Array works in MySQL?
- MySQL supports for storage of data values in the tables where de-normalized columns or say arrays are indexed to manage and access through MySQL queries. In genuine, MySQL does not contain any actual array type storage. As MySQL is also useful in Data Warehousing so, this is a fundamental need but it provides problem in designs for storage of de-normalized rows. However, we can access them using indexes.
- Basically, an Array is defined as an assembly of items that are stored at adjacent memory locations where multiple items of identical types can be stored together. It structures as rows and columns design manner which presents multiplication concepts. In array, any objects, values, numbers or links are arranged in columns and rows type. This is similar to the data records stored in a database table in the form of rows and columns.
- Therefore, in a structure or, storage space when equivalent groups are organized in equivalent rows then an array is designed. So, our using the WHERE IN clause we can fetch the rows and columns from the database table array by following and validating a condition to result required set of output table in the MySQL server.
- But MySQL provides solution to store arrays since MySQL v5.7 by implementation of JSON data type in MySQL, which is quite advance querying process. For now, we are illustrating the WHERE IN array in MySQL server through the examples provided below.
- For array in MySQL with WHERE IN clause, the MySQL WHERE clause is applied together with the function IN() that is responsible to affect only those rows whose column values will match the list of values passed as parameters in the IN keyword. Also, using IN() function with WHERE clause decreases the number of OR keywords or clauses that may be used in the query sometimes.
Examples of MySQL WHERE IN Array
Given below are the examples mentioned:
In MySQL, we implement the clause WHERE IN to pass an array in the database. Let us illustrate MySQL WHERE IN Array with a valid example by creating a table with some entries into it and performing the query.
Initially, let us built up a table providing name as DemoArray with the help of following examples:
CREATE TABLE DemoArray(ID INT PRIMARY KEY, PersonName VARCHAR (255) NOT NULL);
Again, we will insert few records with the help of INSERT command as follows:
INSERT INTO DemoArray VALUES (1,'Nikhil'), (2,'Rita'), (3,'Poonam'), (4,'Pragya'), (5,'Meenu'), (6,'Sandhya'), (7,'Anita');
Now, displaying the table contents by using the SELECT statement below:
SELECT * FROM DemoArray;
Next is the code structure written below which sends an array parameter using the WHERE IN clause in MySQL:
SELECT * FROM DemoArray WHERE ID IN(1,3,6);
From this the output of MySQL WHERE IN Array is as follows:
Or, you may also try like this one below:
SELECT * FROM DemoArray WHERE PersonName IN('Nikhil', 'Meenu', 'Anita');
From this the output of MySQL WHERE IN Array is as follows:
Again, let us demonstrate using another example, suppose we have a table in our database named as books having fields BookID, BookName, Language and Price as follows:
SELECT * FROM Books;
As you can see there are some records are inserted into the Books table.
Subsequent is the command structure inscribed below which shows an array result type using the WHERE IN clause in MySQL. Assume that we want to filter the rows form the table where the book ids are ranged in the IN() parameters.
SELECT * FROM Books WHERE BookID IN(101,103,106);
As you can see the resultant output provides array type design of rows and columns which is filtered using WHERE clause from the table.
Again, we can query using price column and applying condition to it to get the specified matching result of values from the records. The MySQL WHERE IN Array command can be coded as follows.
SELECT BookID, BookName, Language FROM Books WHERE Price IN(5000, 2000,1500);
Thus, query is helpful to retrieve certain part of records from large collection of data in the database in the MySQL server in the form of array structure using condition also to filter the matching values and further checking this with listed ranges of data values from specific table applying IN() function.
With MySQL WHERE clause we query result rows that is filtered using conditional expression defining a rule to fetch values. This filtration process includes any single value, sub query or a range. The parameters passed out in the IN() function follows range of items that when used gives the array like structure in rows and columns format so this array type data is MySQL WHERE IN array.
This is a guide to MySQL WHERE IN Array. Here we discuss the introduction, how WHERE IN array works in MySQL? and examples respectively. You may also have a look at the following articles to learn more –