
- 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 create a MySQL view that takes the values from a table based on some condition(s)?
If we want to create a view that takes the values from a table based on some particular condition(s) then we have to use WHERE clause while creating the view. The values depending upon the WHERE clause will be stored in view. The syntax of creating a MySQL view with WHERE clause can be as follows −
Syntax
Create View view_name AS Select_statements FROM table WHERE condition(s);
Example
To illustrate the above concept, we are using the following data from table ‘Student_info’ −
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 | +------+---------+------------+------------+ 4 rows in set (0.08 sec)
Now, with the help of the following query, we will create the view names ‘Info’ having the condition that it stores the rows having only computers as the subject. So we need to use WHERE clause as follows while creating the view −
mysql> Create OR Replace VIEW Info AS Select Id, Name, Address, Subject from student_info WHERE Subject = 'Computers'; Query OK, 0 rows affected (0.46 sec) mysql> Select * from info; +------+-------+---------+-----------+ | Id | Name | Address | Subject | +------+-------+---------+-----------+ | 125 | Raman | Shimla | Computers | | 130 | Ram | Jhansi | Computers | +------+-------+---------+-----------+ 2 rows in set (0.00 sec)
- Related Articles
- How can I export values based on some conditions from MySQL table into a file?
- How can we create a MySQL view by selecting some range of values from a base table?
- Delete only some rows from a table based on a condition in MySQL
- How can we create a MySQL view based on another existing view?
- How can I create a stored procedure to select values on the basis of some conditions from a MySQL table?
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- How can I search data from MySQL table based on similar sound values?
- How can we copy data with some condition/s from existing MySQL table?
- How can we create MySQL view by selecting data based on pattern matching from base table?
- How can I create a stored procedure to delete values from a MySQL table?
- 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?
- How can I create a stored procedure to insert values in a MySQL table?
- How can I create a stored procedure to update values in a MySQL table?
- How can I create a table and insert values in that table using prepare statements?

Advertisements