
- 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 we create MySQL view by selecting data based on pattern matching from base table?
MySQL LIKE operator is used to select data based on pattern matching. Similarly, we can use LIKE operator with views to select particular data based on pattern matching from the base table. To understand this concept we are using the base table ‘student_info’ having the following data −
mysql> Select * from Student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 125 | Raman | Shimla | Computers | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | +------+---------+------------+------------+ 6 rows in set (0.00 sec)
Example
Following query will create a view named ‘Info’, to select some particular values based on pattern matching by using ‘LIKE’ operator −
mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name LIKE '%Ra%'; Query OK, 0 rows affected (0.11 sec) mysql> Select * from Info; +------+--------+------------+------------+ | id | Name | Address | Subject | +------+--------+------------+------------+ | 105 | Gaurav | Chandigarh | Literature | | 125 | Raman | Shimla | Computers | | 130 | Ram | Jhansi | Computers | +------+--------+------------+------------+ 3 rows in set (0.00 sec)
We can use NOT with LIKE operator also as follows −
mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name NOT LIKE'%Ra%'; Query OK, 0 rows affected (0.14 sec) mysql> Select * from info; +------+---------+------------+-----------+ | id | Name | Address | Subject | +------+---------+------------+-----------+ | 101 | YashPal | Amritsar | History | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | +------+---------+------------+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- How can we create a MySQL view by selecting some range of values from a base table?
- How can we create a MySQL view based on another existing view?
- Selecting data from a MySQL table based on a specific month?
- How can we create a MySQL view by using data from multiple tables?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- Selecting from a MySQL table based on parts of a timestamp?
- How can I create a MySQL view that takes the values from a table based on some condition(s)?
- How can I search data from MySQL table based on similar sound values?
- How can we create a MySQL view with GROUP BY clause?
- How can we create the MySQL view with ORDER BY clause?
- How can you select data from a table based on some criteria using MySQL in Python?
- How to create a table from view in MySQL?
- Create a MySQL table from already created table selecting specific rows?
- How can we create a MySQL table by using PHP script?
- How can we import data from .txt file into MySQL table?

Advertisements