Home  >  Article  >  Database  >  Examples of implementing conditional restriction statements in mysql (1)

Examples of implementing conditional restriction statements in mysql (1)

黄舟
黄舟Original
2017-09-09 14:54:271568browse

1.between statement:

mysql> select * from 4a where score between 76 and 89;
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 钱二   |   19 | 叶平   |    1 | 语文   |    2 |    1 |    79 |
| 钱二   |   19 | 贺高   |    2 | 数学   |    2 |    2 |    81 |
| 张三   |   17 | 杨艳   |    3 | 英语   |    3 |    3 |    88 |
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
| 王五   |   17 | 杨艳   |    3 | 英语   |    5 |    3 |    78 |
+--------+------+--------+------+--------+------+------+-------+
6 rows in set (0.02 sec)
mysql> select * from 4a where score between 78 and 88;
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 钱二   |   19 | 叶平   |    1 | 语文   |    2 |    1 |    79 |
| 钱二   |   19 | 贺高   |    2 | 数学   |    2 |    2 |    81 |
| 张三   |   17 | 杨艳   |    3 | 英语   |    3 |    3 |    88 |
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
| 王五   |   17 | 杨艳   |    3 | 英语   |    5 |    3 |    78 |
+--------+------+--------+------+--------+------+------+-------+
6 rows in set (0.00 sec)

From these two examples, we can see that the value range of the between clause when executed includes its boundary value.

2.or statement:

mysql> select * from 4a where score=78 or score=88 or score>88 or score<60;
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 叶平   |    1 | 语文   |    1 |    1 |    56 |
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 刘一   |   18 | 周磊   |    4 | 物理   |    1 |    4 |    58 |
| 钱二   |   19 | 杨艳   |    3 | 英语   |    2 |    3 |    92 |
| 张三   |   17 | 叶平   |    1 | 语文   |    3 |    1 |    91 |
| 张三   |   17 | 贺高   |    2 | 数学   |    3 |    2 |    47 |
| 张三   |   17 | 杨艳   |    3 | 英语   |    3 |    3 |    88 |
| 张三   |   17 | 周磊   |    4 | 物理   |    3 |    4 |    56 |
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
| 李四   |   18 | 杨艳   |    3 | 英语   |    4 |    3 |    90 |
| 李四   |   18 | 周磊   |    4 | 物理   |    4 |    4 |    93 |
| 王五   |   17 | 叶平   |    1 | 语文   |    5 |    1 |    46 |
| 王五   |   17 | 杨艳   |    3 | 英语   |    5 |    3 |    78 |
| 王五   |   17 | 周磊   |    4 | 物理   |    5 |    4 |    53 |
| 赵六   |   19 | 叶平   |    1 | 语文   |    6 |    1 |    35 |
| NULL   | NULL | NULL   | NULL | NULL   | NULL | NULL |    93 |
+--------+------+--------+------+--------+------+------+-------+
16 rows in set (0.00 sec)

Through this example we can see that there can be many or clauses in a mysql statement.

3.in statement:

mysql> select * from 4a where score in (78,93);
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 刘一   |   18 | 贺高   |    2 | 数学   |    1 |    2 |    78 |
| 李四   |   18 | 周磊   |    4 | 物理   |    4 |    4 |    93 |
| 王五   |   17 | 杨艳   |    3 | 英语   |    5 |    3 |    78 |
| NULL   | NULL | NULL   | NULL | NULL   | NULL | NULL |    93 |
+--------+------+--------+------+--------+------+------+-------+
4 rows in set (0.00 sec)
mysql> select * from 4a where score in (score>60);
Empty set (0.00 sec)

It seems that the in statement can only match a specific record or records within the brackets, but cannot logically match them.

mysql> select * from 4a where score>85;
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 钱二   |   19 | 杨艳   |    3 | 英语   |    2 |    3 |    92 |
| 张三   |   17 | 叶平   |    1 | 语文   |    3 |    1 |    91 |
| 张三   |   17 | 杨艳   |    3 | 英语   |    3 |    3 |    88 |
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
| 李四   |   18 | 杨艳   |    3 | 英语   |    4 |    3 |    90 |
| 李四   |   18 | 周磊   |    4 | 物理   |    4 |    4 |    93 |
| NULL   | NULL | NULL   | NULL | NULL   | NULL | NULL |    93 |
+--------+------+--------+------+--------+------+------+-------+
7 rows in set (0.00 sec)

Obviously the function of where is much more powerful than in, so the command priority of where is also higher. Under the same conditions, the search range of where is slightly larger than that of in - after all, with more functions, there are more things to search for. Therefore, under the same query conditions, the efficiency of where will be slightly worse than that of in, of course, only a little bit. Of course, since the command priority of in is lower than where, SQL statements are more likely to report errors when using in.

and statement:

mysql> select * from 4a where (score>85 and sage=18) and t=2;
+--------+------+--------+------+--------+------+------+-------+
| sname  | sage | tname  | t    | cname  | s    | c    | score |
+--------+------+--------+------+--------+------+------+-------+
| 李四   |   18 | 贺高   |    2 | 数学   |    4 |    2 |    88 |
+--------+------+--------+------+--------+------+------+-------+
1 row in set (0.00 sec)

Through this example we can see that there can be many and clauses in a mysql statement.

The above is the detailed content of Examples of implementing conditional restriction statements in mysql (1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn