Select*fromStudent_info;+------+---------+--------- ---+----------------+|id |Name |Address |Sub"/> Select*fromStudent_info;+------+---------+--------- ---+----------------+|id |Name |Address |Sub">

Home >Database >Mysql Tutorial >How can we create a MySQL view by selecting some range of values ​​from a base table?

How can we create a MySQL view by selecting some range of values ​​from a base table?

PHPz
PHPzforward
2023-08-27 21:45:03769browse

我们如何通过从基表中选择某些范围的值来创建 MySQL 视图?

We know that MySQL BETWEEN operator can be used to select a value from a range of values. We can use the BETWEEN operator with views to select a certain range of values ​​from the base table. To understand this concept, we use the base table "student_info" with 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

The following query will create a view named "Info" by using the "BETWEEN" operation operator to select certain values ​​within a specific range -

mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name BETWEEN 'C' AND 'P';
Query OK, 0 rows affected (0.14 sec)

mysql> Select * from info;
+------+--------+------------+------------+
| id   | Name   | Address    | Subject    |
+------+--------+------------+------------+
| 105  | Gaurav | Chandigarh | Literature |
| 133  | Mohan  | Delhi      | Computers  |
+------+--------+------------+------------+
2 rows in set (0.00 sec)

Similarly, we can use NOT with the BETWEEN operator to select a range that is different from the values ​​written in the query-

mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name NOT BETWEEN 'C' AND 'P';
Query OK, 0 rows affected (0.06 sec)

mysql> Select * from Info;
+------+---------+------------+-----------+
| id   | Name    | Address    | Subject   |
+------+---------+------------+-----------+
| 101  | YashPal | Amritsar   | History   |
| 125  | Raman   | Shimla     | Computers |
| 130  | Ram     | Jhansi     | Computers |
| 132  | Shyam   | Chandigarh | Economics |
+------+---------+------------+-----------+
4 rows in set (0.00 sec)

The above is the detailed content of How can we create a MySQL view by selecting some range of values ​​from a base table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete