For MySQL VIEW with WHERE clause, the syntax is as follows −
select * from yourViewName where yourColumnName='yourValue';
Let us first create a −
mysql> create table DemoTable1432 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentBranchName varchar(20) -> ); Query OK, 0 rows affected (1.26 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1432(StudentName,StudentBranchName) values('Chris','CS'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1432(StudentName,StudentBranchName) values('David','CE'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1432(StudentName,StudentBranchName) values('Mike','ME'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select −
mysql> select * from DemoTable1432;
This will produce the following output −
+-----------+-------------+-------------------+ | StudentId | StudentName | StudentBranchName | +-----------+-------------+-------------------+ | 1 | Chris | CS | | 2 | David | CE | | 3 | Mike | ME | +-----------+-------------+-------------------+ 3 rows in set (0.00 sec)
Following is the query to create view −
mysql> create view DemoTable1432_View as select * from DemoTable1432; Query OK, 0 rows affected (0.17 sec)
Following is the query to use MySQL view with where clause −
mysql> select * from DemoTable1432_View where StudentBranchName='CS';
This will produce the following output −
+-----------+-------------+-------------------+ | StudentId | StudentName | StudentBranchName | +-----------+-------------+-------------------+ | 1 | Chris | CS | +-----------+-------------+-------------------+ 1 row in set (0.03 sec)